R使用具有多个输入的plyr revalue创建函数

R creating a function using plyr revalue with multiple inputs

我是 R 的新手,刚刚起步,所以在此先感谢您提供的任何帮助。

我有一个正在作为 class 项目清理的数据集。

我有几组分类数据要转换成特定的数值。

我正在为不同的列重复相同的代码格式,我认为它会发挥很好的作用。

我想转这个:

# plyr using revalue
df$Area <- revalue(x = df$Area,
                   replace = c("rural" = 1,
                               "suburban" = 2,
                               "urban" = 3))

df$Area <- as.numeric(df$Area)

进入这个:

 reval_3 <- function(data, columnX,
                     value1, num_val1,
                     value2, num_val2,
                     value3, num_val3) {

  # plyr using revalue
  data$columnX <- revalue(x = data$columnX,
                        replace = c(value1 = num_val1,
                                    value2 = num_val2,
                                    value3 = num_val3))

  # set as numeric
  data$columnX <- as.numeric(data$columnX)

  # return dataset
  return(data)

}

我收到以下错误:

The following `from` values were not present in `x`: value1, value2, value3
Error: Assigned data `as.numeric(data$columnX)` must be compatible with existing data.
x Existing data has 10000 rows.
x Assigned data has 0 rows.
ℹ Only vectors of size 1 are recycled.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning messages:
1: Unknown or uninitialised column: `columnX`. 

我用一个值 1 试过了,其中值 1 <- c("rural" = 1, "suburban" = 2, "urban" = 3)

我知道我可以:

df$Area <- as.numeric(as.factor(df$Area))

数据,但我想要每个选择的特定值而不是 R 选择。

感谢任何帮助。

正如@MartinGal 在他的评论中提到的那样,plyr 已停用,包作者自己建议改用 dplyr。参见 https://github.com/hadley/plyr

因此,实现您想要的结果的一种选择是利用 dplyr::recode。此外,如果您想编写函数,我建议将值传递给重新编码并将替换作为向量传递,而不是将每个值和替换作为单独的参数传递:

library(dplyr)

set.seed(42)

df <- data.frame(
  Area = sample(c("rural", "suburban", "urban"), 10, replace = TRUE)
)

recode_table <- c("rural" = 1, "suburban" = 2, "urban" = 3)

recode(df$Area, !!!recode_table)
#>  [1] 1 1 1 1 2 2 2 1 3 3

reval_3 <- function(data, x, values, replacements) {
  recode_table <- setNames(replacements, values)  
  data[[x]] <- recode(data[[x]], !!!recode_table)
  data
}

df <- reval_3(df, "Area", c("rural", "suburban", "urban"), 1:3)
df
#>    Area
#> 1     1
#> 2     1
#> 3     1
#> 4     1
#> 5     2
#> 6     2
#> 7     2
#> 8     1
#> 9     3
#> 10    3

您可以将 case_whenacross 一起使用。

如果您要更改的列名为 col1col2,您可以执行 -

library(dplyr)

df <- df %>%
  mutate(across(c(col1, col2), ~case_when(. == 'rural' ~ 1, 
                          . == 'suburban' ~ 2, 
                          . == 'urban' ~ 3)))

根据您的实际列名,您还可以在 across 中传递 starts_withends_with、列范围 A:Z