【发布时间】:2019-07-05 17:01:14
【问题描述】:
我有两个数据框,一宽一长:
long_df = structure(list(PID = c(1001, 1001, 1001, 1002, 1002, 1002, 1002,
1003), scan_name = c("01_001A", "01_001B", "01_001C", "01_002A",
"01_002B", "01_002D", "01_002E", "01_003B")), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
wide_df = structure(list(PID = c(1001, 1002, 1003), scan_name_1 = c("01_001A",
"01_002A", NA), scan_date_1 = structure(c(1206748800, 1240876800,
NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), scan_name_2 = c("01_001B",
"01_002B", "01_003B"), scan_date_2 = structure(c(1238544000,
1272672000, 1424736000), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
scan_name_3 = c("01_001C", NA, NA), scan_date_3 = structure(c(1301702400,
NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
scan_name_4 = c(NA, "01_002D", NA), scan_date_4 = structure(c(NA,
1400112000, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
scan_name_5 = c(NA, "01_002E", NA), scan_date_5 = structure(c(NA,
1430438400, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
我正在尝试将值“scan_date_1”、“scan_date_2”等从wide_df 获取到long_df。
我试图得到的输出如下所示:
goal_df = structure(list(PID = c(1001, 1001, 1001, 1002, 1002, 1002, 1002,
1003), scan_name = c("01_001A", "01_001B", "01_001C", "01_002A",
"01_002B", "01_002D", "01_002E", "01_003B"), scan_date = structure(c(1206748800,
1238544000, 1301702400, 1240876800, 1272672000, 1400112000, 1430438400,
1424736000), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
看起来很简单,但我使用合并/融化/等的尝试都没有成功。非常感谢任何和所有帮助! (第一次使用“dput”,所以希望这是一个可复制的例子)
【问题讨论】:
-
我认为你需要通过 'PID' 加入
-
你能发布预期的输出吗?
-
我已经编辑了我的帖子以添加目标输出,谢谢@RuiBarradas