使用一个条件

时间:2017-05-25 13:23:33

标签: r insert conditional-statements countif

我有一个包含三列的数据框,第一个是id,第二个是事件A发生时,第三个列是事件B发生时。

client <- rep(c("A","B","C"),5)
event_a <- c("2017-04-02","2017-04-07","2017-04-10", "2017-04-13","2017-04-
17","2017-04-18","2017-04-20","2017-04-22","2017-04-25","2017-04-27","2017-
04-30","2017-05-10","2017-05-12","2017-05-15","2017-05-20")
event_b <- c("2017-04-05",NA,"2017-04-11", "2017-04-14",NA,"2017-04-
20","2017-04-21",NA,"2017-04-30",NA,"2017-04-30",NA,NA,"2017-05-15",NA)
data <- data.frame(client, event_a, event_b)

我想知道客户端是否在事件B之后发生事件A,或者事件B是否为NA - 布尔变量还是计数1。但我喜欢在data.frame中插入此列,以便在第二时刻进行建模。

谢谢!

2 个答案:

答案 0 :(得分:0)

我想你只想要这个

data$logic_value<-!is.na(data$event_b)

答案 1 :(得分:0)

4