Let’s say I have a data frame, like this:
df <- data.frame(
variable = rep(letters[1:10], 2),
y2 = 1:10,
y1 = c(10, 9, 8 ,7, 6, 5, 4, 2, 1, 3),
stat = c(rep(letters[1], 10), rep(letters[2], 10))
)
By “stat”, I would like to create three new columns, one that shows a numbered rank for y1 and y2, and another that calculates the change in rank between y1 and y2 (short for year 1 and year 2).
I’ve been tinkering with ddply, but I can’t seem to get it to do what I want. Here’s an example of what I’ve tried (which may also illustrate what I’m attempting to do):
ddply(df, .(stat), function(x) data.frame(
df,
y1rank = rank(x$x),
y2rank = rank(x$y),
change = rank(x$y) - rank(x$x)
))
Would this work for you?