當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R dplyr recode 重新編碼值


[Superseded]

recode()case_match() 取代,它以更優雅的接口處理 recode() 最重要的情況。 recode_factor() 也被取代,但是,其直接替代目前不可用,但最終將存在於 forcats 中。要基於邏輯向量創建新變量,請使用 if_else() 。對於更複雜的標準,請使用 case_when()

recode()switch() 的矢量化版本:您可以根據位置或名稱替換數值,僅根據名稱替換字符或因子值。這是 S3 泛型:dplyr 提供數字、字符和因子的方法。您可以直接使用recode() 與因子;它將在更改值時保留現有的級別順序。或者,您可以使用 recode_factor() ,這將更改級別的順序以匹配替換的順序。

用法

recode(.x, ..., .default = NULL, .missing = NULL)

recode_factor(.x, ..., .default = NULL, .missing = NULL, .ordered = FALSE)

參數

.x

要修改的向量

...

<dynamic-dots> 替換。對於字符和因子 .x ,應命名它們,並且僅根據其名稱進行替換。對於數字 .x ,可以命名也可以不命名。如果未命名,則替換是根據位置完成的,即 .x 表示在替換中查找的位置。請參閱示例。

命名時,參數名稱應為要替換的當前值,參數值應為新(替換)值。

所有替換必須是相同類型,並且長度必須為 1 或與 .x 相同的長度。

.default

如果提供,則所有未以其他方式匹配的值都將被賦予該值。如果未提供並且替換值與 .x 中的原始值類型相同,則不更改不匹配的值。如果未提供並且替換不兼容,則不匹配的值將替換為 NA

.default 的長度必須為 1 或與 .x 的長度相同。

.missing

如果提供,.x 中的任何缺失值都將替換為該值。長度必須為 1 或與 .x 相同的長度。

.ordered

如果 TRUErecode_factor() 創建一個有序因子。

.x 長度相同的向量,並且與 ....default.missing 中的第一個向量相同的類型。 recode_factor() 返回一個因子,其級別的順序與 ... 中的順序相同。 .default.missing 中的級別排在最後。

也可以看看

na_if()NA 替換指定值。

coalesce() 用指定值替換缺失值。

tidyr::replace_na()NA 替換為值。

例子

char_vec <- sample(c("a", "b", "c"), 10, replace = TRUE)

# `recode()` is superseded by `case_match()`
recode(char_vec, a = "Apple", b = "Banana")
#>  [1] "Banana" "c"      "Banana" "Banana" "c"      "Banana" "Apple" 
#>  [8] "Banana" "Banana" "Banana"
case_match(char_vec, "a" ~ "Apple", "b" ~ "Banana", .default = char_vec)
#>  [1] "Banana" "c"      "Banana" "Banana" "c"      "Banana" "Apple" 
#>  [8] "Banana" "Banana" "Banana"

# With `case_match()`, you don't need typed missings like `NA_character_`
recode(char_vec, a = "Apple", b = "Banana", .default = NA_character_)
#>  [1] "Banana" NA       "Banana" "Banana" NA       "Banana" "Apple" 
#>  [8] "Banana" "Banana" "Banana"
case_match(char_vec, "a" ~ "Apple", "b" ~ "Banana", .default = NA)
#>  [1] "Banana" NA       "Banana" "Banana" NA       "Banana" "Apple" 
#>  [8] "Banana" "Banana" "Banana"

# Throws an error as `NA` is logical, not character.
try(recode(char_vec, a = "Apple", b = "Banana", .default = NA))
#> Error in recode(char_vec, a = "Apple", b = "Banana", .default = NA) : 
#>   `.default` must be a character vector, not `NA`.

# `case_match()` is easier to use with numeric vectors, because you don't
# need to turn the numeric values into names
num_vec <- c(1:4, NA)
recode(num_vec, `2` = 20L, `4` = 40L)
#> [1]  1 20  3 40 NA
case_match(num_vec, 2 ~ 20, 4 ~ 40, .default = num_vec)
#> [1]  1 20  3 40 NA

# `case_match()` doesn't have the ability to match by position like
# `recode()` does with numeric vectors
recode(num_vec, "a", "b", "c", "d")
#> [1] "a" "b" "c" "d" NA 
recode(c(1,5,3), "a", "b", "c", "d", .default = "nothing")
#> [1] "a"       "nothing" "c"      

# For `case_match()`, incompatible types are an error rather than a warning
recode(num_vec, `2` = "b", `4` = "d")
#> Warning: Unreplaced values treated as NA as `.x` is not compatible.
#> Please specify replacements exhaustively or supply `.default`.
#> [1] NA  "b" NA  "d" NA 
try(case_match(num_vec, 2 ~ "b", 4 ~ "d", .default = num_vec))
#> Error in case_match(num_vec, 2 ~ "b", 4 ~ "d", .default = num_vec) : 
#>   Can't combine `..1 (right)` <character> and `.default` <integer>.

# The factor method of `recode()` can generally be replaced with
# `forcats::fct_recode()`
factor_vec <- factor(c("a", "b", "c"))
recode(factor_vec, a = "Apple")
#> [1] Apple b     c    
#> Levels: Apple b c

# `recode_factor()` does not currently have a direct replacement, but we
# plan to add one to forcats. In the meantime, you can use the `.ptype`
# argument to `case_match()`.
recode_factor(
  num_vec,
  `1` = "z",
  `2` = "y",
  `3` = "x",
  .default = "D",
  .missing = "M"
)
#> [1] z y x D M
#> Levels: z y x D M
case_match(
  num_vec,
  1 ~ "z",
  2 ~ "y",
  3 ~ "x",
  NA ~ "M",
  .default = "D",
  .ptype = factor(levels = c("z", "y", "x", "D", "M"))
)
#> [1] z y x D M
#> Levels: z y x D M
源代碼:R/recode.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Recode values。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。