I have two vectors:
x<-c(0,1,0,2,3,0,1,1,0,2)
y<-c("00:01:00","00:02:00","00:03:00","00:04:00","00:05:00",
"00:06:00","00:07:00","00:08:00","00:09:00","00:10:00")
I need to choose only those in y
, where values of x
is not interrupted by 0. As a result, I'd like to get a dataframe like this
y x
00:04:00 2
00:05:00 3
00:07:00 1
00:08:00 1
We built a script like this, but with a big dataset it takes time. Is there a more elegant solution? And I wonder, why df<-rbind(bbb,df)
returns inverted df?
aaa<-data.frame(y,x)
df<-NULL
for (i in 1:length(aaa$x)){
bbb<-ifelse((aaa$x[i]*aaa$x[i+1])!=0,
aaa$x[i],
ifelse((aaa$x[i]*aaa$x[i-1])!=0,
aaa$x[i],
NA))
df<-rbind(bbb,df)
}
df<-data.frame(rev(df))
aaa$x<-df$rev.df.
bbb<-na.omit(aaa)
bbb
I'm a newbie in R, so please, as much detail as you can :) Thank you!