Hi,
dat1$colIndex<-apply(dat1,1,function(x) match(max(x),x))
?dat1
#? v1 v2 v3 v4 max colIndex
#1? 0? 0? 1? 2?? 2??????? 4
#2? 0? 1? 2? 0?? 2??????? 3
#3? 1? 2? 3? 4?? 4??????? 4
A.K.
----- Original Message -----
From: Camilo Mora <cmora at dal.ca>
To: r-help at r-project.org
Cc:
Sent: Saturday, June 8, 2013 8:14 PM
Subject: [R] get column index with match using apply
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
dtv1? 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/
______________________________________________
R-help at 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.