【发布时间】:2021-03-28 22:20:48
【问题描述】:
我有两个要合并的带有时间戳的数据集。对于df2 中X 列中的每个值,我想从df1 中获得Jar 和Treatment 的值。从df1 我可以看到在特定时间测量了哪个Jar 以及Treatment 是什么。在df2 中,我可以看到X 在特定时间的值是多少,当我看到X 的特定值时,我需要知道Jar(以及哪个Treatment)被测量。
我对@987654334@ 进行了一些尝试,但由于时间序列中存在间隙,这不起作用。例如。在 df2 中有一个值 X 在时间戳:2020-12-16 14:31:05,但此时间戳在 df1 中不存在。然而,基于 df1,我知道在这个时间戳 Jar=Soil_dry 和 Treatment=None。
关于如何制作一个表格,我可以在 df1 中为 df2 中的每个 X 值获取 Jar 和 Treatment 值的任何建议?
这里是 df1:
df1 <- structure(list(Jar = c("Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry",
"Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry",
"Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry",
"Soil_dry", "Soil_dry", "Soil_dry", "soil_wet", "soil_wet", "soil_wet",
"soil_wet", "soil_wet", "soil_wet", "soil_wet", "soil_wet", "soil_wet",
"soil_wet", "soil_wet", "soil_wet", "soil_wet", "soil_wet", "soil_wet",
"soil_wet", "soil_wet", "soil_wet", "soil_wet", "soil_wet", "Soil_dry",
"Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry",
"Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry", "Soil_dry",
"Soil_dry", "soil_wet", "soil_wet", "soil_wet", "soil_wet", "soil_wet",
"soil_wet", "soil_wet", "soil_wet", "soil_wet", "soil_wet", "soil_wet",
"soil_wet", "soil_wet", "soil_wet", "soil_wet"), Treatment = c("None",
"None", "None", "None", "None", "None", "None", "None", "None",
"None", "None", "None", "None", "None", "None", "None", "None",
"None", "None", "None", "None", "None", "None", "None", "None",
"None", "None", "None", "None", "None", "None", "None", "None",
"None", "None", "None", "None", "None", "None", "ul5", "ul5",
"ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5",
"ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5",
"ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5", "ul5"
), Timestamp = structure(c(1608128877, 1608128933, 1608128997,
1608129058, 1608129063, 1608129112, 1608129117, 1608129122, 1608129127,
1608129238, 1608129243, 1608129348, 1608129353, 1608129358, 1608129363,
1608129368, 1608129373, 1608129473, 1608129478, 1608129483, 1608129488,
1608129598, 1608129603, 1608129717, 1608129723, 1608129837, 1608129842,
1608129957, 1608129962, 1608130072, 1608130077, 1608130082, 1608130192,
1608130197, 1608130202, 1608130318, 1608130323, 1608130418, 1608130423,
1608130428, 1608130492, 1608130497, 1608130502, 1608130507, 1608130612,
1608130617, 1608130622, 1608130627, 1608130732, 1608130737, 1608130742,
1608130747, 1608130853, 1608130858, 1608130863, 1608130978, 1608130983,
1608131093, 1608131098, 1608131103, 1608131213, 1608131218, 1608131223,
1608131337, 1608131343, 1608131457, 1608131462, 1608131467), class = c("POSIXct",
"POSIXt"), tzone = "UTC")), row.names = c(NA, -68L), class = "data.frame")
df2:
df2 <-structure(list(X = c(5L, 3L, 34L, 4L, 65L, 5L, 7L, 5L, 8L, 9L,
8L, 5L, 78L, 9L, 5L, 78L, 9L, 5L, 78L, 9L, 5L, 7L, 4L, 34L, 8L,
5L, 4L, 9L, 78L, 59L, 5L, 5L, 6L, 3L, 3L, 7L, 5L, 47L, 2L, 67L,
34L, 76L, 5L, 76L, 5L, 6L, 5L, 7L, 2L, 13L, 1L, 54L, 32L, 4L,
3L, 45L, 1L, 1L), Timestamp = structure(c(1608129065, 1608129122,
1608129127, 1608129238, 1608129263, 1608129288, 1608129353, 1608129358,
1608129363, 1608129368, 1608129373, 1608129473, 1608129478, 1608129483,
1608129488, 1608129598, 1608129663, 1608129717, 1608129723, 1608129831,
1608129842, 1608129957, 1608129962, 1608130072, 1608130073, 1608130082,
1608130132, 1608130197, 1608130202, 1608130318, 1608130323, 1608130418,
1608130423, 1608130428, 1608130492, 1608130497, 1608130502, 1608130507,
1608130612, 1608130617, 1608130622, 1608130627, 1608130732, 1608130737,
1608130742, 1608130747, 1608130853, 1608130858, 1608130863, 1608130978,
1608130983, 1608131093, 1608131098, 1608131103, 1608131213, 1608131218,
1608131223, 1608131337), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), row.names = c(NA, -58L), class = "data.frame")
【问题讨论】:
-
由于 df1 和 df2 只有“时间戳”列共有,您是否尝试通过“时间戳”列合并两者?
-
merge(df1, df2)能给你想要的吗? -
merge(df1, df2)的问题是缺少时间戳。例如,df2 在 2020-12-16 14:27:52 的值为 X,但由于 df1 中不存在此时间戳,因此merge(df1, df2)的结果中不存在 X 测量值。