簡體   English   中英

如何使用 R 變異 function 轉換 NA 值?

[英]How to transform NA values with the R mutate function?

我正在嘗試使用 function mutate是為了根據其他三個條件創建一個變量。 這些條件是使用case_when創建的,您可以在下面的代碼中看到。

但我有一些使用 NA 值的條件,這些似乎導致mutate function 出現錯誤。 請檢查一下:

# 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)}.")

有人可以給我一個關於如何解決這個問題的提示嗎?

這里的問題是你的case_when的結果。 if_else 形式 dplyr 比基礎 R 的 ifelse 更嚴格 - 所有結果值都必須屬於同一類型。 由於 case_when 是多個 if_else 的矢量化,因此您必須告訴 R output 應該是哪種類型的 NA:

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 具有不同類型的 NA。 您使用的是邏輯類型,但您需要雙重類型 NA_real_ 以便與您的其他條件的 output 保持一致。 有關更多信息,請參閱: https://stat.ethz.ch/R-manual/R-patched/library/base/html/NA.html

您需要確保您的NA是正確的 class。 在您的情況下,將NA放在as.numeric()中的~之后。 例如:

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

base R中,我們可以構造一個邏輯向量並根據該邏輯向量將列值分配給NA case_when不同,我們不必真正指定NA的類型,因為它會自動轉換。

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

此外,為了簡單的操作,可以在base R中以緊湊的方式完成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM