我需要为每个组提供最新时间值的行。我有多个行和列。输入数据如下:
view(DF)
ID Value1 Value2 Value3 Value4 Time
1 1 7 13 19 2013-11-15 21:12:03:337
1 2 8 14 20 2013-12-23 15:12:01:227
2 3 9 15 21 2014-12-07 14:37:01:127
2 4 10 16 22 2013-12-12 05:23:01:239
3 5 11 17 23 2011-12-25 15:12:01:227
3 6 12 18 24 2011-12-25 15:12:02:227
我需要如下结果:
ID Value1 Value2 Value3 Value4 Time
1 2 8 14 20 2013-12-23 15:12:01:227
2 3 9 15 21 2014-12-07 14:37:01:127
3 6 12 18 24 2011-12-25 15:12:02:227
我已经查看了community.rstudio.com/t/replacing-all-null-values-in-a-dataset-code-issue/11285中提供的解决方案
我尝试过
DF_filter = DF%>%
group_by(ID) %>%
slice(which.max(Time))
此外,我试图将时间戳中的时间列转换为
DF_time <- strptime(x=as.character(DF$Time), format = "%y-%m-%d %H%M:S")
但是DF_time返回NA
第一个代码返回的数据(DF_filter
)具有0磅。和6个变量。
请帮忙。
答案 0 :(得分:3)
一种<div class="relative">
text
<div class="absolute">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim recusandae doloribus nesciunt unde vitae quis aliquid laborum adipisci ipsa, dolorem repellendus nulla iure atque minus fuga sunt rem eaque animi.</div>
text
</div>
方法
样本数据
.relative {
position: relative;
}
.absolute {
position: absolute;
background-color: grey;
max-width: 100%;
top: 100%;
left: 50%;
transform: translateX(-50%);
}
代码
data.table
输出
DT <- fread('ID Value1 Value2 Value3 Value4 Time
1 1 7 13 19 "2013-11-15 21:12:03:337"
1 2 8 14 20 "2013-12-23 15:12:01:227"
2 3 9 15 21 "2014-12-07 14:37:01:127"
2 4 10 16 22 "2013-12-12 05:23:01:239"
3 5 11 17 23 "2011-12-25 15:12:01:227"
3 6 12 18 24 "2011-12-25 15:12:02:227"', quote = "\"")
答案 1 :(得分:2)
正确格式化时间变量后:
使用base
:
do.call(rbind, lapply(
split(df, df$ID),
function(x) x[which.max(x$Time), ]
))
或者:
merge(aggregate(Time ~ ID, data = df, FUN = max), df)
答案 2 :(得分:1)
impl