根据条件指定值1或0

时间:2019-04-16 21:15:29

标签: r if-statement count unique

我有从10月到4月的月度报告,并汇总了所有数据。我先按UniqueID排序数据,然后按日期排序。

我想创建一个满足以下条件的虚拟变量:

1。)如果最后一次出现的特定UniqueID不在上个月(4月),那么我希望变量为= 1,否则为0。

“频率”列计算唯一ID在堆叠的月度报告的整个数据集中显示的次数。

UniqueID Date        Freq
XX343_1  02/01/2019  3
XX343_1  03/01/2019  3  
XX343_1  04/01/2019  3
SD229_1  11/01/2018  4 
SD229_1  12/01/2018  4
SD229_1  01/01/2019  4
SD229_1  02/01/2019  4
WE321_1  10/01/2018  1

基本上,我想要以下输出:

UniqueID Date        Freq Dummy
XX343_1  02/01/2019  3    0
XX343_1  03/01/2019  3    0
XX343_1  04/01/2019  3    0
SD229_1  11/01/2018  4    0
SD229_1  12/01/2018  4    0
SD229_1  01/01/2019  4    0
SD229_1  02/01/2019  4    1
WE321_1  10/01/2018  1    1

以下是我尝试的代码:

 data$Dummy=ifelse(data$Date=="2018-10-01" & data$Freq==1,1,ifelse(
                   data$Date=="2018-10-01" & data$Freq>=2,0,ifelse(
                   data$Date=="2018-11-01" & data$Freq<=2,1,ifelse(
                   data$Date=="2018-11-01" & data$Freq >2,0,ifelse(
                   data$Date=="2018-12-01" & data$Freq<=3,1,ifelse(
                   data$Date=="2018-12-01" & data$Freq >3,0,ifelse(
                   data$Date=="2019-01-01" & data$Freq<=4,1,ifelse(
                   data$Date=="2019-01-01" & data$Freq >4,0,ifelse(
                   data$Date=="2019-02-01" & data$Freq<=5,1,ifelse(
                   data$Date=="2019-02-01" & data$Freq >5,0,ifelse(
                   data$Date=="2019-03-01" & data$Freq<=6,1,ifelse(
                   data$Date=="2019-03-01" & data$Freq >6,0,0
               ))))))))))))

我不断收到错误消息,但不确定如何解决问题。在很多情况下,如果第一次出现唯一ID的时间不是在十月,则虚拟对象在第二个月到上个月将为0。有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

library(dplyr); library(lubridate)
data <- read.table(header = T, stringsAsFactors = F,
  text = "UniqueID Date        Freq
  XX343_1  02/01/2019  3
  XX343_1  03/01/2019  3  
  XX343_1  04/01/2019  3
  SD229_1  11/01/2018  4 
  SD229_1  12/01/2018  4
  SD229_1  01/01/2019  4
  SD229_1  02/01/2019  4
  WE321_1  10/01/2018  1"
) %>% 
  mutate(Date = mdy(Date))

ID_dummy <- data %>%
  group_by(UniqueID) %>%
  summarize(last_Date = max(Date))

data %>%
  left_join(ID_dummy) %>%
  mutate(Dummy = if_else(last_Date == Date & month(last_Date) != 4, 1, 0))
#Joining, by = "UniqueID"
#  UniqueID       Date Freq  last_Date Dummy
#1  XX343_1 2019-02-01    3 2019-04-01     0
#2  XX343_1 2019-03-01    3 2019-04-01     0
#3  XX343_1 2019-04-01    3 2019-04-01     0
#4  SD229_1 2018-11-01    4 2019-02-01     0
#5  SD229_1 2018-12-01    4 2019-02-01     0
#6  SD229_1 2019-01-01    4 2019-02-01     0
#7  SD229_1 2019-02-01    4 2019-02-01     1
#8  WE321_1 2018-10-01    1 2018-10-01     1