[R] loop help

ddepew at sciborg.uwaterloo.ca ddepew at sciborg.uwaterloo.ca
Wed Jun 17 05:12:02 CEST 2009


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!

-- 
David Depew
PhD Candidate
Department of Biology
University of Waterloo
200 University Ave W
Waterloo, Ontario, Canada
N2L 3G1

T:(1)-519-888-4567 x 33895
F:(1)-519-746-0614

ddepew at scimail.uwaterloo.ca
http://www.science.uwaterloo.ca/~ddepew




More information about the R-help mailing list