【问题标题】:creating new variable using conditional mutating returns NAs for a subset of rows使用条件变异创建新变量返回行子集的 NA
【发布时间】:2017-01-15 00:56:24
【问题描述】:

我正在尝试根据日期变量的逻辑条件创建一个新的因子变量:

  'data.frame': 364458 obs. of  2 variables:

   $ first_order_date: Date, format: "2015-11-24" "2015-12-15" "2015-06-10" "2015-12-22" ...
   $ order_date     : Date, format: "2016-02-09" "2016-03-15" "2015-12-22" "2015-12-28" ...



library(moisaic)

bb =      
 df %>%
    mutate(days_since_first = as.integer(order_date - first_order_date),
    time_after_first =  derivedFactor(
                        "<3months" = order_date <= first_order_date +months(3),
                        "3-6months" = (order_date <= first_order_date +months(6) & order_date > first_order_date +months(3)),

...

                        "15-18months" = (order_date <= first_order_date +months(18) & order_date > first_order_date +months(15)),
                        "18-21months" = (order_date <= first_order_date +months(21) & order_date > first_order_date +months(18)),
                        .default = "21month+"))

运行后,我收到警告:

 Warning messages:
 1: In base::max(x, ..., na.rm = na.rm) :
 no non-missing arguments to max; returning -Inf

在大多数情况下它仍然工作得很好,

 > head(bb[!is.na(bb$time_after_first), ])

         first_order_date order_date   days_since_first time_after_first
    1         2015-11-24  2016-02-09                  77         <3months
    2         2015-12-15  2016-03-15                  91         <3months
    3         2015-06-10  2015-12-22                 195        6-9months
    4         2015-12-22  2015-12-28                   6         <3months
    5         2016-01-21  2016-05-29                 129        3-6months
    6         2016-03-12  2016-05-30                  79         <3months

但不是全部

 sum(is.na(bb$time_after_first))
 [1] 7174

我看不出为什么这些特定条目不起作用的任何模式

summary(bb[is.na(bb$time_after_first), ])

    first_order_date      order_date         days_since_first
   Min.   :2015-01-31   Min.   :2015-01-31    Min.   :  0.0   
   1st Qu.:2015-08-31   1st Qu.:2016-02-07    1st Qu.: 41.5   
   Median :2015-11-30   Median :2016-03-31    Median :106.0   
   Mean   :2015-11-05   Mean   :2016-03-12    Mean   :128.3   
   3rd Qu.:2016-01-31   3rd Qu.:2016-05-13    3rd Qu.:175.0   
   Max.   :2016-05-31   Max.   :2016-09-04    Max.   :546.0   

  time_after_first
 <3months   :   0   
 3-6months  :   0   
 6-9months  :   0   
 9-12months :   0   
 12-15months:   0   
 (Other)    :   0   
 NA's       :6455

另外,我尝试使用普通的 ifelse() 语句来实现这一点,

 bb2 = 
  all_orders3 %>% select(user_id, order_id, first_order_date, order_date2,  category) %>% 
  mutate(days_since_first = as.integer(order_date - first_order_date),
       time_after_first=  as.factor(ifelse(order_date <=   first_order_date +months(3), "<3months", 
                               ifelse(order_date <= first_order_date +months(6) & order_date > first_order_date +months(3), "3-6months",


 ....

 ifelse(order_date <= first_order_date +months(24) & order_date >    first_order_date +months(21), "21-24months",
                                                                                    "24months+"))))))))))

没有收到警告,但生成了更多的 NA,仍然没有明确的模式为什么会发生这种情况

 summary(bb2[is.na(bb2$time_after_first), ])

      first_order_date      order_date          days_since_first
     Min.   :2015-01-31   Min.   :2015-01-31     Min.   :  0.0   
     1st Qu.:2015-08-31   1st Qu.:2016-02-10     1st Qu.: 52.0   
     Median :2015-11-30   Median :2016-04-08     Median :123.0   
     Mean   :2015-10-27   Mean   :2016-03-25     Mean   :150.2   
     3rd Qu.:2016-01-31   3rd Qu.:2016-05-29     3rd Qu.:211.0   
     Max.   :2016-05-31   Max.   :2016-09-04     Max.   :582.0   

     time_after_first
     <3months   :   0   
     12-15months:   0   
     15-18months:   0   
     18-21months:   0   
     3-6months  :   0   
     (Other)    :   0   
     NA's       :7407   

这是link 以及我的数据样本

欢迎任何有用的建议如何克服这个问题,谢谢!

【问题讨论】:

  • 请展示一个小的可重现示例(显示问题)以及其他人测试您的代码的预期输出
  • 我刚刚添加了一个带有有效记录的摘录(head(bb[!is.na(bb$time_after_first), ])),还有什么可以帮助的吗?
  • 如果您使用bb1 &lt;- head(bb[!is.na(bb$time_after_first), ]) 上的代码会出现同样的错误吗?这是为了测试问题到底出在哪里
  • 是的,它重复错误
  • 只是出于好奇,这是什么derivedFactor 东西?貌似是函数调用,什么包?

标签: r if-statement conditional dplyr na


【解决方案1】:

为示例 v2 更新:

#Read csv...
dat = read.csv("data/07092016-sample_V2.csv")

library(dplyr)
dat = dat %>%
  mutate(t1 = as.Date(first_order_date, "%d/%m/%Y"),
         t2 = as.Date(order_date, "%d/%m/%Y"),
         days = as.numeric(difftime(t2, t1, units = "days"))) %>% 
  select(t1:days)

head(dat)
#           t1         t2 days
# 1 2016-03-02 2016-05-29   88
# 2 2015-04-25 2015-05-01    6
# 3 2015-06-29 2015-07-04    5
# 4 2015-09-09 2016-02-05  149
# 5 2016-01-08 2016-02-15   38
# 6 2016-04-17 2016-04-21    4

breaks = c(-Inf, 90, 180, Inf)
labels = c("<3 months", "3-6 months", "6+ months")

days = cut(dat$days, breaks, labels)
sum(is.na(days))
# [1] 0

【讨论】:

  • pffff,不知道发生了什么。但是非常感谢您对解决方案的方向进行调查和暗示...... :)
猜你喜欢
  • 2015-11-14
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多