基于R dplyr/tidyverse数据帧的多列获取最大日期



从如下的csv文件:

名称>对象参数属性1Atrib2结果<1>o1Nap<2td>TP>n01a2氧气NapTP输出36673.09mg/l<1>o3TPOUTmg/l>o4<2td>BOD<10><220>[/tr>mg/l>o4<2td>BOD<1td>TPINmg/lo1<2td>NO2<1td>TPOUT>毫克/升>IOIN毫克/升>INo1>TPIN17909n01a2o2TP输出19216.19
日期 时间戳 单位条件
2019-07-31 2019-08-01 01:16:09 m3 n01134937
2019-07-31 2019-08-01 01:16:10 m3
2019-11-06 2019-11-18 20:21:06n01a3NO3
2019-11-06 2019-11-18 20:21:06n01z5IN
2019-11-06 2019-11-18 20:21:06n01z5
2019-11-06 2019-11-18 20:21:06n01z60.31
2019-11-06 2019-11-18 20:21:13n01a11o4Ntot
2019-11-06 2019-11-18 20:21:13n01a11o4NtotTP
2021-01-06 2021-01-07 02:15:06 m3 n01 a1Nap
2021-01-06 2021-01-07 02:15:07 m3Nap

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))

相关内容

  • 没有找到相关文章

最新更新