[R] get column index with match using apply

Camilo Mora cmora at dal.ca
Sun Jun 9 02:14:06 CEST 2013


Hi everyone,
This is probably something very simple but I have just not been able  
to figure it out and wonder if perhaps you may know.

I want to get the column index in which a given value is found. Values  
to match are found in the last column of the dataframe. for instance

dt=
v1   v2   v3   v4    max
0    0    1    2     2
0    1    2    0     2
1    2    3    4     4

expected output:
v1   v2   v3   v4    max colIndex
0    0    1    2     2     4
0    1    2    0     2     3
1    2    3    4     4     4

I am using:

apply(dt, 1, function(x) match(max,x))

but there is an error in setting "max" in match to be dynamic for each  
row. I have play around with different configurations and google it  
with no success.
Any input will be greatly appreciated,

best,

Camilo


Camilo Mora, Ph.D.
Department of Geography, University of Hawaii
Currently available in Colombia
Phone:   Country code: 57
          Provider code: 313
          Phone 776 2282
          From the USA or Canada you have to dial 011 57 313 776 2282
http://www.soc.hawaii.edu/mora/



More information about the R-help mailing list