从如下的csv文件:
日期 | 时间戳 | 单位 | 名称>条件 | 对象参数属性1Atrib2结果|||
---|---|---|---|---|---|---|
2019-07-31 | 2019-08-01 01:16:09 | m3 | n01 | <1>o1Nap<2td>TP>134937 | ||
2019-07-31 | 2019-08-01 01:16:10 | m3 | n01a2氧气NapTP输出36673.09||||
2019-11-06 | 2019-11-18 20:21:06 | mg/l<1>n01 | a3 | o3NO3 | TPOUT||
2019-11-06 | 2019-11-18 20:21:06 | mg/ln01 | z5 | >o4<2td>BOD<10>IN | <220>[/tr>||
2019-11-06 | 2019-11-18 20:21:06 | mg/ln01 | z5 | >o4<2td>BOD<1td>TPIN|||
2019-11-06 | 2019-11-18 20:21:06 | mg/ln01 | z6 | o1<2td>NO2<1td>TPOUT>0.31 | ||
2019-11-06 | 2019-11-18 20:21:13 | 毫克/升n01 | a11 | o4 | >Ntot | IOIN|
2019-11-06 | 2019-11-18 20:21:13 | 毫克/升n01 | a11 | o4 | >Ntot | TP | IN
2021-01-06 | 2021-01-07 02:15:06 | m3 | n01 | a1 | o1>Nap | TPIN17909|
2021-01-06 | 2021-01-07 02:15:07 | m3 | n01a2o2Nap | TP输出19216.19
max(Timestamp)
中有多个值。为了解决这个问题,我建议使用dplyr::slice_max
并设置with_ties = FALSE
。
这里有一些代码来获取您想要的内容。
df %>%
mutate(Date = as.POSIXct(Date, format = "%Y-%m-%d")) %>%
mutate(Timestamp = as.POSIXct(Timestamp, format = "%Y-%m-%d %H:%M:%S")) %>%
group_by(Date, Condition) %>%
slice_max(order_by = Timestamp, n = 1, with_ties = FALSE)
但是,根据您的应用程序,您可能希望明确如何通过向order_by
参数提供额外的变量来解决这些联系。
尝试使用以下方法:
library(dplyr)
read.csv("./Example.csv") %>%
#df %>%
mutate(Date = as.Date(Date),
Timestamp = as.POSIXct(Timestamp, format = "%Y-%m-%d %H:%M:%S")) %>%
distinct(Date, Condition, Result, .keep_all = TRUE) -> result
result
# Date Timestamp Units Name Condition Obj Param Attrib1 Atrrib2 Result
#1 2019-07-31 2019-08-01 01:16:09 m3 n01 a1 o1 Nap TP IN 34937.00
#2 2019-07-31 2019-08-01 01:16:10 m3 n01 a2 o2 Nap TP OUT 36673.09
#3 2019-11-06 2019-11-18 20:21:06 mg/l n01 a3 o3 NO3 TP OUT 1.00
#4 2019-11-06 2019-11-18 20:21:06 mg/l n01 z5 o4 BOD IO IN 220.00
#5 2019-11-06 2019-11-18 20:21:06 mg/l n01 z6 o1 NO2 TP OUT 0.31
#6 2019-11-06 2019-11-18 20:21:13 mg/l n01 a11 o4 Ntot IO IN 47.00
#7 2021-01-06 2021-01-07 02:15:06 m3 n01 a1 o1 Nap TP IN 17909.00
#8 2021-01-06 2021-01-07 02:15:07 m3 n01 a2 o2 Nap TP OUT 19216.19
数据
df <- structure(list(Date = c("2019-07-31", "2019-07-31", "2019-11-06",
"2019-11-06", "2019-11-06", "2019-11-06", "2019-11-06", "2019-11-06",
"2021-01-06", "2021-01-06"), Timestamp = c("2019-08-01 01:16:09",
"2019-08-01 01:16:10", "2019-11-18 20:21:06", "2019-11-18 20:21:06",
"2019-11-18 20:21:06", "2019-11-18 20:21:06", "2019-11-18 20:21:13",
"2019-11-18 20:21:13", "2021-01-07 02:15:06", "2021-01-07 02:15:07"
), Units = c("m3", "m3", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l",
"mg/l", "m3", "m3"), Name = c("n01", "n01", "n01", "n01", "n01",
"n01", "n01", "n01", "n01", "n01"), Condition = c("a1", "a2",
"a3", "z5", "z5", "z6", "a11", "a11", "a1", "a2"), Obj = c("o1",
"o2", "o3", "o4", "o4", "o1", "o4", "o4", "o1", "o2"), Param = c("Nap",
"Nap", "NO3", "BOD", "BOD", "NO2", "Ntot", "Ntot", "Nap", "Nap"
), Attrib1 = c("TP", "TP", "TP", "IO", "TP", "TP", "IO", "TP",
"TP", "TP"), Atrrib2 = c("IN", "OUT", "OUT", "IN", "IN", "OUT",
"IN", "IN", "IN", "OUT"), Result = c(34937, 36673.09, 1, 220,
220, 0.31, 47, 47, 17909, 19216.19)),class = "data.frame",row.names = c(NA,-10L))