On 17/06/2009, at 3:12 PM, dde...@sciborg.uwaterloo.ca wrote:

Hi all,
I'm stuck trying to get syntax correct for the follwing type of loop.
I would like to find the column with the largest value in a given row,
and create a new column with a categorical variable indicating which
column the highest value of "i" comes from.

too=data.frame(A=rnorm(10,1),B=rnorm(10,2),C=rnorm(10,1.5))
too$large=0
too$large=for (i in 1:length(too[,c(1,2,3)])) {
                                    if (too$A[i] > too$B[i] &
too$C[i]) {too$large[i]=1} else
                                    if (too$B[i] > too$A[i] &
too$C[i]) {too$large[i]=2} else
                                    if (too$C[i] > too$A[i] &
too$B[i]) {too$large[i]=3}}

Ultiamtely this is not working for me, any hints would be much appreciated!

Hints:

(1) Use the tools and facilities that R provides. Take some time to learn
about them.  It will pay off, big-time.

(2) Whatever anyone else may tell you, ***DON'T*** use ``='' for assignment.
That way lies peril.  Use ``<-'' as God intended. :-)

(3) The solution to your immediate problem is exemplified by:

set.seed(42)
too <- data.frame(A=rnorm(10,1),B=rnorm(10,2),C=rnorm(10,1.5))
too$large <- apply(too,1,which.max)
too
           A          B          C large
1  2.3709584  3.3048697  1.1933614     2
2  0.4353018  4.2866454 -0.2813084     2
3  1.3631284  0.6111393  1.3280826     1
4  1.6328626  1.7212112  2.7146747     3
5  1.4042683  1.8666787  3.3951935     3
6  0.8938755  2.6359504  1.0695309     2
7  2.5115220  1.7157471  1.2427306     1
8  0.9053410 -0.6564554 -0.2631631     1
9  3.0184237 -0.4404669  1.9600974     1
10 0.9372859  3.3201133  0.8600051     2

        cheers,

                Rolf Turner


######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

______________________________________________
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.

Reply via email to