Reputation: 7846
I have a dataframe with one column containing factors in a sequence of variable lengths and other columns containing values. How can I extract just the last row values for each factor?
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
Thank you for your help.
Upvotes: 0
Views: 1862
Reputation: 132706
There are numerous alternatives. This is not the most efficient, but it doesn't need a package:
aggregate(. ~ x, df, tail, n = 1)
Upvotes: 5