我有一个数据框,其中一列包含一系列可变长度的因子和包含值的其他列。如何只提取每个因子的最后一行值?
df <- data.frame(x=c("a","a","a","a","b","b","c","c","c","c","c","d","d","d","e","f","f","f","f","g","g"),y=c(diffinv(rnorm(20))),z=c(diffinv(rnorm(20))))
df
感谢您的帮助。
答案 0 :(得分:5)
有很多选择。这不是最有效的,但它不需要包:
aggregate(. ~ x, df, tail, n = 1)