Skip to content
Prev 321917 / 398500 Next

select and do some calculations/manipulations on certain rows based on conditions in R

Hi,
May be this helps (Assuming that there are only '0's and '1's in the dataset)

dat1<-read.table(text="
??????? ID X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15
1?? 5184??? 0??? 0??? 0??? 0??? 0?? 0?? 0??? 0??? 0??? 1???? 0????? 0????? 0????? 0????? 0????? 0
2?? 6884??? 0??? 0??? 1??? 0??? 0?? 1?? 0??? 0??? 0??? 0???? 0????? 0????? 0????? 0????? 0????? 0
3? 11329?? 0??? 0??? 0??? 0??? 0?? 1?? 0??? 0??? 0??? 0???? 0????? 0????? 0????? 0????? 0????? 0
4? 11329?? 0??? 0??? 1??? 0??? 0?? 1?? 0??? 0??? 0??? 0???? 0????? 0????? 0????? 0????? 0????? 0 
",sep="",header=TRUE)
dat1$y<-sapply(seq_len(nrow(dat1)),function(i) {x1<-na.omit(dat1[i:(i+1),]); 1*any(colSums(x1[,-1])==2)})
?dat1
#???? ID X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 y
#1? 5184? 0? 0? 0? 0? 0? 0? 0? 0? 0? 1?? 0?? 0?? 0?? 0?? 0?? 0 0
#2? 6884? 0? 0? 1? 0? 0? 1? 0? 0? 0? 0?? 0?? 0?? 0?? 0?? 0?? 0 1
#3 11329? 0? 0? 0? 0? 0? 1? 0? 0? 0? 0?? 0?? 0?? 0?? 0?? 0?? 0 1
#4 11329? 0? 0? 1? 0? 0? 1? 0? 0? 0? 0?? 0?? 0?? 0?? 0?? 0?? 0 0
A.K.
Message-ID: <1366312155.70355.YahooMailNeo@web142601.mail.bf1.yahoo.com>
In-Reply-To: <CAE7utsvwjfVDAbx6mBM=VrJmbth=yxhJjrHoo6Bz1uVwons+8Q@mail.gmail.com>