[R] choose the lines

arun smartpink111 at yahoo.com
Tue May 28 16:01:08 CEST 2013


HI GG,
Try this:

dat2<- fun1(dat.bru)
res1<-aggregate(basdai_d~patient_id+evnmt_brutal,data=dat2,mean)
res11<-res1[order(res1$patient_id),]
row.names(res11)<- 1:nrow(res11)
head(res11,10)
#   patient_id evnmt_brutal   basdai_d
#1           2            0  0.9000000
#2           2            1 -0.8000000
#3           3            0 -0.7083333
#4           3            1  0.5750000
#5           5            0  0.0000000
#6           5            1 -1.2000000
#7           6            0  0.7750000
#8           6            1 -0.4250000
#9           7            0 -1.7250000
#10          7            1  0.8250000

#or
library(plyr)
res2<-ddply(dat2,.(patient_id,evnmt_brutal),summarize,basdai_d=mean(basdai_d))
identical(res11,res2)
#[1] TRUE
A.K.


________________________________
From: GUANGUAN LUO <guanguanluo at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Tuesday, May 28, 2013 4:53 AM
Subject: Re: choose the lines



Thanks a lot, Arun, you are soooooo great!!!

If i want to get the mean when evnmt_brutal==0 and  evnmt_brutal==1 for each patient, what can i do for that?

GG



2013/5/27 arun <smartpink111 at yahoo.com>


>
>Hi,
>Try this:
>dat1<- read.csv("dat7.csv",header=TRUE,stringsAsFactors=FALSE,sep="\t")
>dat.bru<- dat1[!is.na(dat1$evnmt_brutal),]
>
>fun1<- function(dat){   
>      lst1<- split(dat,dat$patient_id)
>    lst2<- lapply(lst1,function(x) x[cumsum(x$evnmt_brutal==0)>0,])
>    lst3<- lapply(lst2,function(x) x[!(all(x$evnmt_brutal==1)|all(x$evnmt_brutal==0)),])
>    lst4<-lapply(lst3,function(x) {vect.brutal=c()
>                for(line in which(x$evnmt_brutal==1)){
>                   if(x$evnmt_brutal[line-1]==0){
>                  vect.brutal=c(vect.brutal,line)
>                    }
>                       }
>                  vect.brutal1<- sort(c(vect.brutal,vect.brutal-1))
>                 x[vect.brutal1,]
>                       }
>                       )
>    res<- do.call(rbind,lst4)
>    row.names(res)<- 1:nrow(res)
>    res
>    }
>
>
>fun1(dat.bru)head(fun1(dat.bru),10)
>#    X patient_id number responsed_at  t basdai_d evnmt_brutal
>#1  14          2     13   2011-08-07 13    0.900            0
>#2  15          2     14   2011-09-11 14   -0.800            1
>#3  22          3      2   2010-06-29  1   -0.800            0
>#4  23          3      3   2010-08-05  2    0.000            1
>#5  24          3      4   2010-09-05  3    1.200            0
>#6  25          3      5   2010-10-13  4    1.925            1
>#7  26          3      6   2010-11-15  5   -2.525            0
>#8  27          3      7   2010-12-18  6   -0.200            1
>#9  53          5      9   2011-02-13  8    0.000            0
>#10 54          5     10   2011-03-19  9   -1.200            1
>
>
>A.K.
>
>_______________________________
>From: GUANGUAN LUO <guanguanluo at gmail.com>
>To: arun <smartpink111 at yahoo.com>
>Sent: Monday, May 27, 2013 8:48 AM
>Subject: choose the lines
>
>
>
>
>Hello, Arun,
>
>in this data, i want to choose every line with the variable " evnmt_brutal"==1 & the precedent line( line-1) with "evnmt_brutal"==0,
>i had done this,
>
>res.bru <- dat7[!is.na(dat7$evnmt_brutal),]
>vect.brutal=c()
>for(line in which(res.bru$evnmt_brutal==1)){
>  if(res.r$evnmt_brutal[line-1]==0){
>    vect.brutal=c(vect.brutal,line)}
>}
>vect.brutal
>
>but now i think it's not correct. Because if there are the situations just like this
>Patient_id      evnmt_brutal
>1                      ...
>1                      ...
>1                      0
>2                      1
>2                      ...
>2                      ...
>
>I would have chosen the lines of two different patients, so that is not correct.
>Do you know how can i change a little and get the correct lines just for each patient?
>
>Thank you so much.
>
>GG
>



More information about the R-help mailing list