[R] Extracting data from dataframe with tied rows

rjb robertjohnburnside at hotmail.com
Thu Aug 23 23:19:06 CEST 2012


Hi R help,

I'm a fairly experienced R user but this manipulation has me stumped, please
help:

DATA
id<-rep(LETTERS[1:5],20)
distance<-rnorm(1:100, mean = 100)
bearing<-sample(1:360,100,replace=T)
month<-sample(1:12,100,replace=T)

I have a dataset with records of individuals (id) , each with a distance
(distance) & direction (bearing) recorded for each month (month).
I want to find the largest distance per individual per month, which is easy
with /tapply/ or /melt/cast (reshape)/,
head(DATA_m<-melt(DATA,id=c("id","month")))
cast(DATA_m,id+month~.,max)
OR
na.omit(melt(tapply(distance,list(id,month),max)))

*BUT THE CATCH IS* ,
I also want the the *corresponding*  bearing for that maximum distance per
month. I've tried the steps above plus using which.max() and loops, but
can't solve the problem. The real dataset is about 6000 rows.

I'm guessing the answer is in finding the row number from the original DATA
but I can't figure how to do that with tapply or melt.

Any suggestions would be greatly appreciated.

John Burnside





--
View this message in context: http://r.789695.n4.nabble.com/Extracting-data-from-dataframe-with-tied-rows-tp4641140.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list