# [?]apply functions or for loop

3 messages
Open this post in threaded view
|

## [?]apply functions or for loop

 Hello, First time posting to this mail list. I'd like to use R in the most efficient way. I'm accomplishing what I want, but feel there is a more R'ish way to do it. Just learning R. *My goal: get ranks of value across rows with row names and column names intact.* I'm guessing one of the [?]apply functions will do what I need, but I couldn't sort out which one (after a lot of searching). Here is a simplified example of what I am doing. Again, I get the correct result, but I assume there is a better way to do it. > x = data.frame(1:4,4) > x   X1.4 X4 1    1  4 2    2  4 3    3  4 4    4  4 > ranks = matrix(0,nrow(x),ncol(x)) > ranks      [,1] [,2] [1,]    0    0 [2,]    0    0 [3,]    0    0 [4,]    0    0 > for(i in 1:nrow(x)){ + ranks[i,] = rank(x[i,]) + } > ranks[i,] [1] 1.5 1.5 > ranks      [,1] [,2] [1,]  1.0  2.0 [2,]  1.0  2.0 [3,]  1.0  2.0 [4,]  1.5  1.5 > rownames(ranks) = rownames(x) > ranks   [,1] [,2] 1  1.0  2.0 2  1.0  2.0 3  1.0  2.0 4  1.5  1.5 > rownames(ranks) = rownames(x) > ranks   [,1] [,2] 1  1.0  2.0 2  1.0  2.0 3  1.0  2.0 4  1.5  1.5 > colnames(ranks) = colnames(x) > ranks   X1.4  X4 1  1.0 2.0 2  1.0 2.0 3  1.0 2.0 4  1.5 1.5 Thanks, Ben         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.