adam.888
adam.888

Reputation: 7846

R extracting the last row values of each factor in a dataframe

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

Answers (1)

Roland
Roland

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

Related Questions