我想计算value
显示>20
从前一个ncol = 1
列(5
)的数字1定义的特定日期1997.05.25
的次数天。例如,日期>20
值显示1997.05.21
到value <- c(37,19.75,19.5,14.5,24.75,25,25.5,19.75,19.75,14.25,21.25,21.75,17.5,16.25,14.5,
14.5,14.75,9.5,11.75,15.25,14.25,16.5,13.5,18.25,13.5,11.25,10.75,12,8.5,
9.75,14.75)
Date <- c("1997-05-01","1997-05-02","1997-05-03","1997-05-04","1997-05-05",
"1997-05-06","1997-05-07","1997-05-08","1997-05-09","1997-05-10",
"1997-05-11","1997-05-12","1997-05-13","1997-05-14","1997-05-15",
"1997-05-16","1997-05-17","1997-05-18","1997-05-19","1997-05-20",
"1997-05-21","1997-05-22","1997-05-23","1997-05-24","1997-05-25",
"1997-05-26","1997-05-27","1997-05-28","1997-05-29","1997-05-30",
"1997-05-31")
ncol <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)`
data <- data.frame(value, Date, ncol)
library(data.table)
setDT(data)[, ncol := ifelse(Date %in% c("1997-05-05","1997-05-11","1997-05-14",
"1997-05-18","1997-05-25"), ncol+1, 0) ]
的次数。
以下是变量:
1. value Date ncol newcol
2. 37.00 1997-05-05 1 2
3. 19.75 1997-05-11 1 2
4. 19.50 1997-05-14 1 2
5. 14.50 1997-05-18 1 0
6. 24.75 1997-05-25 1 0
如何做到最好?非常感谢帮助。
这是我的预期结果:
UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(navigateToPreviousScreen:)];
rightSwipeGesture.cancelsTouchesInView = YES;
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipeGesture];
答案 0 :(得分:1)
如果我理解正确,您可以使用Reduce
和shift
:
library(data.table)
DT <- data.table(value, Date)
DT[, ncol := Reduce(`+`, shift(value > 20, 1:5, fill = 0))]
这样做:对于DT的每一行,它计算前5行中value
- 列大于20的时间,并将其写入列ncol
。
如果你想改变这个5排&#34;窗口&#34;从前5天开始包括当前日期+前4行,您可以将代码更改为:
DT[, ncol := Reduce(`+`, shift(value > 20, 0:4, fill = 0))]
答案 1 :(得分:0)
以下是使用dplyr
的解决方案:
library(dplyr)
data$Date <- as.Date(data$Date)
data %>%
filter(between(Date, "1997-05-01", "1997-05-21"),
value > 20) %>%
nrow()