You should have been able to discern from the help pages that the generic "apply" will do it.
E.g., apply(x,1,rank) Now, you'll probably want to transpose the output of apply: it's a R quirk/feature/bug/idiosyncrasy that apply(x,1,FUN) transposes the output and most of the time I wind up switching it. so r = t(apply(x,1,rank)) The syntax of apply is as follows: apply(THING TO APPLY, DIMENSION*, FUNCTION, OTHERS) THING TO APPLY -- Obvious DIMENSION -- 1 for row-wise, 2 for column-wise FUNCTION -- the function you are applying, here rank, without any arguments or parentheses OTHERS -- other arguments for more advanced functions (or to do things like na.last=F or changing the ties.method with rank) Hope this helps, Michael Weylandt On Fri, Aug 5, 2011 at 11:35 AM, Ben qant <ccqu...@gmail.com> wrote: > 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]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.