当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。