Say I have a data frame, for example:
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))
)
In the "stat" section, I would like to create three new columns that show the numbered rank for y1and y2, and another that calculates the change in rank between y1and y2(short for year and year 2).
I trained with ddply, but I can't get him to do what I want. Here is an example of what I tried (which may also illustrate what I'm trying 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)
))
source
share