忽略(但不删除)dplyr group_by函数中的NA

时间:2019-03-21 06:55:52

标签: r

previous post中,我得到了基于放置在多个其他变量上的条件来更改单个变量的帮助。

但是,由于我在分组变量中有多个缺失值,因此出现了进一步的复杂情况。数据框示例如下:

df2 <- data.frame(
  ID = c(101:110),
  Name = c("AA", "BB", "AA", "DD", "EE", "FF", "AA", "GG", "DD", "HH"),
  Age = c(1, 56, 1, 72, 12, 43, 1, 32, 72, 99),
  Gender = c("F", "M", "F", NA , NA, "M", "F", "M", NA, "M"),
  Group = c(1, 2, 1, 2, 1, 4, 1, 3, 2, 4),
  Date = seq(from = as.Date("2019-01-01"), to = as.Date("2019-01-10"), by = 'day'), 
  Order = c("re-do", "first", "first", "first", "re-do", "first", "re-do", "first", "re-do", "first"),
  Site = c(2, 54, 2, 522, 3, 490, 2, 23, 522, 21)
)

>df2
    ID Name Age Gender Group       Date Order Site
1  101   AA   1      F     1 2019-01-01 re-do    2
2  102   BB  56      M     2 2019-01-02 first   54
3  103   AA   1      F     1 2019-01-03 first    2
4  104   DD  72   <NA>     2 2019-01-04 first  522
5  105   EE  12   <NA>     1 2019-01-05 re-do    3
6  106   FF  43      M     4 2019-01-06 first  490
7  107   AA   1      F     1 2019-01-07 re-do    2
8  108   GG  32      M     3 2019-01-08 first   23
9  109   DD  72   <NA>     2 2019-01-09 re-do  522
10 110   HH  99      M     4 2019-01-10 first   21

我有一个功能,可以根据姓名,年龄,性别和组进行分组,然后根据日期和顺序列更改ID:

library(dplyr)
df2 %>% 
  group_by(Name, Age, Gender, Group, Site) %>%
  mutate(first_date = ifelse(Order == "first", 
                             Date, 
                             Date[Order == "first"])) %>%
  mutate(ID = ifelse(n() > 1 & Date >= first_date, 
                     ID[Order == "first"], 
                     ID)) %>%
  select(-first_date)

但是,我的问题是NA值仍然匹配并使用(请参阅下面第4和9行中复制的ID值):

      ID Name    Age Gender Group Date       Order  Site
   <int> <fct> <dbl> <fct>  <dbl> <date>     <fct> <dbl>
 1   101 AA        1 F          1 2019-01-01 re-do     2
 2   102 BB       56 M          2 2019-01-02 first    54
 3   103 AA        1 F          1 2019-01-03 first     2
 4   104 DD       72 NA         2 2019-01-04 first   522
 5   105 EE       12 NA         1 2019-01-05 re-do     3
 6   106 FF       43 M          4 2019-01-06 first   490
 7   103 AA        1 F          1 2019-01-07 re-do     2
 8   108 GG       32 M          3 2019-01-08 first    23
 9   104 DD       72 NA         2 2019-01-09 re-do   522
10   110 HH       99 M          4 2019-01-10 first    21
Warning messages:
1: Factor `Gender` contains implicit NA, consider using `forcats::fct_explicit_na` 
2: Factor `Gender` contains implicit NA, consider using `forcats::fct_explicit_na` 
3: Factor `Gender` contains implicit NA, consider using `forcats::fct_explicit_na` 
4: Factor `Gender` contains implicit NA, consider using `forcats::fct_explicit_na` 

我想发生的是,带有NA的行被忽略但未被删除(这是我在管道中使用na_omit()设法获得的唯一结果),因此看起来像这样:

    ID Name Age Gender Group       Date Order Site
1  101   AA   1      F     1 2019-01-01 re-do    2
2  102   BB  56      M     2 2019-01-02 first   54
3  103   AA   1      F     1 2019-01-03 first    2
4  104   DD  72   <NA>     2 2019-01-04 first  522
5  105   EE  12   <NA>     1 2019-01-05 re-do    3
6  106   FF  43      M     4 2019-01-06 first  490
7  103   AA   1      F     1 2019-01-07 re-do    2
8  108   GG  32      M     3 2019-01-08 first   23
9  109   DD  72   <NA>     2 2019-01-09 re-do  522
10 110   HH  99      M     4 2019-01-10 first   21

1 个答案:

答案 0 :(得分:1)

我认为对NA列中的Gender值进行额外检查应该能解决问题吗?

library(dplyr)

df2 %>% 
  group_by(Name, Age, Gender, Group, Site) %>%
  mutate(first_date = ifelse(Order == "first", 
                         Date, 
                         Date[Order == "first"]), 
          ID = ifelse(n() > 1 & Date >= first_date & !is.na(Gender), 
                    ID[Order == "first"], 
                    ID)) %>%
   select(-first_date)


#      ID Name    Age Gender Group Date       Order  Site
#   <int> <fct> <dbl> <fct>  <dbl> <date>     <fct> <dbl>
# 1   101 AA        1 F          1 2019-01-01 re-do     2
# 2   102 BB       56 M          2 2019-01-02 first    54
# 3   103 AA        1 F          1 2019-01-03 first     2
# 4   104 DD       72 NA         2 2019-01-04 first   522
# 5   105 EE       12 NA         1 2019-01-05 re-do     3
# 6   106 FF       43 M          4 2019-01-06 first   490
# 7   103 AA        1 F          1 2019-01-07 re-do     2
# 8   108 GG       32 M          3 2019-01-08 first    23
# 9   109 DD       72 NA         2 2019-01-09 re-do   522
#10   110 HH       99 M          4 2019-01-10 first    21