Double Conditioned selection of data in r

只谈情不闲聊 提交于 2019-12-12 02:58:29

问题


I read extensively about selection with condition using function like aggregate or the package pylr but seems ok for my case.

I am sure is not difficult to program, but I would like some input. Basically how to start, what's the line of reasoning that you would follow.

Thanks for any advice.

So my simplified dataset looks like this

time.stamp <- c(21.0,21.1,21.2,21.3,21.4)
behavior <- c("close", "1", "close","1","close")
event_type <- c("start","point","stop","point","start")

example <- data.frame(time.stamp,behavior,event_type)



  time.stamp behavior event_type
1       21.0    close      start
2       21.1        1      point
3       21.2    close       stop
4       21.3        1      point
5       21.4    close      start

My research question is: which is the number of behavior==1 during the behavior==Close.

For example in this case the answer would be 1 Because the second 1 is after a Close&Stop.

Among the other possible solution I thought of subsetting by the range of time.stamps that are in between a close&start and a close&stop but I would not know how to translate this in code.

As I said I would love some input on how to think the problem.

Thanks a lot, I hope to learn something. Cheers


回答1:


I guess to solve this, you don't need any special packages. Using only 'base':

length( which( example$behavior[which( example$behavior == 'close' & as.character(example$event_type) == 'stop')+1] == 1 ) )

EDIT: After clearing what is required output, code changes to:

sum((which( example$behavior == 'close' & as.character(example$event_type) == 'stop') - which( example$behavior == 'close' & as.character(example$event_type) == 'start')) - 1)

Best, Adii_




回答2:


I would do in this way:

n = nrow(example)
length(which(example$behavior[2:n]==1 & example$event_type[1:n-1]!= "stop"))


来源:https://stackoverflow.com/questions/25868401/double-conditioned-selection-of-data-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!