简体   繁体   中英

How to transform NA values with the R mutate function?

I'm trying to use the function mutate is order to create a variable based on conditions regarding three others. These conditions were created using case_when , as you may see in the code below.

But I have some conditions that uses NA valures, and these seems to be causing an error in the mutate function. Check it out, please:

# About the variables being used:

unique(x1)
#   [1]  1  0 NA

str(pemg$x1)
# num [1:1622989] 1 0 0 1 1 0 1 1 0 0 ...

unique(x2)
#   [1]  16  66  38  11   8   6  14  17  53  59  10  31  50  19  48  42  44  21  54  55  56  18  57  61  13  43   7   4  15
#  [30]  39   5  20   3  37  23  51  36  52  68  58  27  65  62   2  12  32  41  49  46  35  34  45  81  69  33  40   0  70
#  [59]   9  47  63  29  25  22  64  24  60  30  67  26  71  72  28   1  75  80  87  77  73  78  76  79  74  83  92 102  85
#  [88]  86  90  82  91  84  88  93  89  96  95 105 115 106  94 100  99  97 104  98 103 108 109 101 117 107 114 113  NA 112
# [117] 110 111

str(pemg$x2)
# num [1:1622989] 16 66 38 11 8 6 14 17 53 59 ...

unique(x3)
#   [1]  6  3  4  5  0  8  2  1 11  9 10  7 NA 15

str(pemg$anoest)
# num [1:1622989] 6 3 4 5 3 0 5 8 4 2 ...

    df <- mutate(df,
                   y = case_when(
                       x1 == 1 & x2 >=  7 & x3 ==  0 ~ 1,
                       x1 == 1 & x2 >=  8 & x3 ==  1 ~ 1,
                       x1 == 1 & x2 >= 10 & x3 ==  3 ~ 1,
                       x1 == 1 & x2 >= 11 & x3 ==  4 ~ 1,
                       x1 == 1 & x2 >= 12 & x3 ==  5 ~ 1,
                       x1 == 1 & x2 >= 13 & x3 ==  6 ~ 1,
                       x1 == 1 & x2 >= 14 & x3 ==  7 ~ 1,
                       x1 == 1 & x2 >= 15 & x3 ==  8 ~ 1,
                       x1 == 1 & x2 >= 16 & x3 ==  9 ~ 1,
                       x1 == 1 & x2 >= 17 & x3 == 10 ~ 1,
                       x1 == 1 & x2 >= 18 & x3 == 11 ~ 1,
                       x1 == 1 & !is.na(x3) ~ 0,
                       x1 == 1 & x3 %in% 12:16 ~ 0,
                       x2 %in% 0:7 ~ NA,
                       x2 > 18 ~ NA,
                       x1 == 0 ~ NA,
                       is.na(x3) ~ NA))

# Error: Problem with `mutate()` input `defasado`.
# x must be a double vector, not a logical vector.
# i Input `defasado` is `case_when(...)`.
# Run `rlang::last_error()` to see where the error occurred.

last_error()
# <error/dplyr_error>
# Problem with `mutate()` input `y`.
# x must be a double vector, not a logical vector.
# i Input `y` is `case_when(...)`.
# Backtrace:
#  1. dplyr::mutate(...)
#  2. dplyr:::mutate.data.frame(...)
#  3. dplyr:::mutate_cols(.data, ...)
#  Run `rlang::last_trace()` to see the full context.

last_trace()
# <error/dplyr_error>
# Problem with `mutate()` input `defasado`.
# x must be a double vector, not a logical vector.
# i Input `defasado` is `case_when(...)`.
# Backtrace:
#     x
#  1. +-dplyr::mutate(...)
#  2. \-dplyr:::mutate.data.frame(...)
#  3.   \-dplyr:::mutate_cols(.data, ...)
# <parent: error/rlang_error>
# must be a double vector, not a logical vector.
# Backtrace:
#     x
#  1. +-mask$eval_all_mutate(dots[[i]])
#  2. \-dplyr::case_when(...)
#  3.   \-dplyr:::replace_with(...)
#  4.     \-dplyr:::check_type(val, x, name)
#  5.       \-dplyr:::glubort(header, "must be {friendly_type_of(template)}, not {friendly_type_of(x)}.")

Can someone give me a hint on how to solve this?

The problem here are the results of your case_when. if_else form dplyr is stricter than ifelse from base R - all result values have to be of the same type. Since case_when is a vecotrization of multiple if_else you have to tell R which type of NA the output should be:

library(dplyr)
# does not work
dplyr::tibble(d = c(6,2,4, NA, 5)) %>% 
  dplyr::mutate(v = case_when(d < 4 ~ 0,
                              is.na(d) ~ NA))
# works
dplyr::tibble(d = c(6,2,4, NA, 5)) %>% 
  dplyr::mutate(v = case_when(d < 4 ~ 0,
                              is.na(d) ~ NA_real_))

R has different types of NA. The one you are using is of logical type, but you need the double type NA_real_ in order to be consistent with the output of your other conditions. For more information, see this: https://stat.ethz.ch/R-manual/R-patched/library/base/html/NA.html

You need to make sure your NA 's are the right class. In your case, place the NA after the ~ in as.numeric() . For example:

x2 %in% 0:7 ~ as.numeric(NA)

In base R , we can construct a logical vector and assign the column values to NA based on that logical vector. Unlike case_when , we don't have to really specify the type of NA as this gets automatically converted.

df1$d[df1$d %in% 0:7] <- NA

Also, for a simple operation, it can be done in base R in a compact way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM