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