跨不同数据帧的两个数字之间的Sumif

时间:2018-02-21 17:26:02

标签: r dataframe dplyr plyr

我有两个数据框。其中一个“从”和“到”的间隔如下;

Intervals <- data.frame("From" = c(0.0000,0.0069,0.0139,0.0208,0.0278,0.0347,0.0417,0.0486,0.0556,0.0625,0.0694,0.0764,0.0833),
                        "To" = c(0.0410,0.0479,0.0549,0.0618,0.0688,0.0757,0.0826,0.0896,0.0965,0.1035,0.1104,0.1174,0.1243))

,第二个数据框是:

x <- data.frame("Dummy" = c(0,1,0,0,0,0,0,0,1,0,0,0,0), 
                "Dummy Time" = c(0,0,0.006944444,0.006944444,0.010416667,0.010416667,0.013888889,0.013888889,0.020833333,0.024305556,0.027777778,0.03125,0.03125))

因此,如果虚拟时间落在间隔df中的From和To(或等于)之间,我基本上想要在虚拟变量的R中进行求和。这在excel中很容易,但对于R来说我是一个新手。

由于

1 个答案:

答案 0 :(得分:0)

library(dplyr)

cbind(Intervals, x)%>%
data.frame()%>%
rowwise()%>%
filter(between(Dummy_Time, From, To))%>%
.$Dummy%>%
sum()

应该做的工作。希望这会有所帮助。