r中数据的双重条件选择

r中数据的双重条件选择

本文介绍了r中数据的双重条件选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我广泛阅读了有关使用 aggregate 或包 pylr 之类的函数进行条件选择的内容,但对我来说似乎没问题.

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.

感谢您的建议.

所以我的简化数据集看起来像这样

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

我的研究问题是:在行为==关闭期间,行为数量==1.

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

例如在这种情况下答案是 1因为第二个 1 是在 Close&Stop 之后.

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

在其他可能的解决方案中,我想到了通过 close&start 和 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

推荐答案

我想解决这个问题,你不需要任何特殊的包.仅使用基础":

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))

清除所需的输出后,代码更改为:

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)

最好的,Adii_

这篇关于r中数据的双重条件选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 19:46