[R] selecting rows by maximum value of one variables in dataframe nested by another Variable

arun smartpink111 at yahoo.com
Wed Jun 27 13:53:59 CEST 2012


HI,

Try this:
dat1 <- read.table(text="
subject    time.ms V3 
1      1  stringA
1      12  stringB
1      22    stringC
2      1    stringB
2      14  stringC
2      25  stringA
", sep="",header=TRUE)
dat2<-aggregate(dat1$time.ms,list(dat1$subject),max)
colnames(dat2)<-c("subject","time.ms")


 merge(dat2,dat1)
  subject time.ms      V3
1       1      22 stringC
2       2      25 stringA

A.K.




----- Original Message -----
From: Miriam <Miriam_Katharina at gmx.de>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, June 26, 2012 5:21 PM
Subject: [R] selecting rows by maximum value of one variables in dataframe nested by another Variable

How could I select the rows of a dataset that have the maximum value in one variable and to do this nested in another variable. It is a dataframe in long format with repeated measures per subject.  
I was not successful using aggregate, because one of the columns has character values (and/or possibly because of another reason).
I would like to transfer something like this: 
subject    time.ms  V3 
1        1    stringA
1        12    stringB
1        22      stringC
2        1     stringB
2        14    stringC
2        25    stringA
…. 
To something like this: 
subject       time.ms    V3
1        22    stringC
2        25     stringA
… 

Thank you very much for you help!
Miriam
-- 

Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a

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




More information about the R-help mailing list