将变量重新编码为R中具有级别名称的因子

时间:2019-03-03 15:34:35

标签: r dataframe mutate r-factor

使用我的数据集 bikeshare ,我想将变量季节重新编码为具有有意义级别名称(例如,“冬季”,“春季”,“夏季”)的因素“,” fall“),以春季为基准水平。

这是我的尝试:

bikeshare <- bikeshare %>%
mutate(season = factor(c(1 = "winter", 
                      2 = "spring", 
                      3 = "summer", 
                      4 = "fall")))    

这是我得到的错误:

Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied 
to an object of class "factor"

我将非常感谢您的帮助,我是一个初学者。

3 个答案:

答案 0 :(得分:0)

这里是如何将数字(1:4)转换为因子(“冬天”,“春天”,“夏天”,“秋天”)的示例。关键是使用factor功能并相应地设置levelslabels

# Create example data frame
bikeshare <- data.frame(season = 1:4)

bikeshare
#   season
# 1      1
# 2      2
# 3      3
# 4      4

library(dplyr)

bikeshare2 <- bikeshare %>%
  mutate(season = factor(as.character(season), 
                         levels = c(1, 2, 3, 4),
                         labels = c("winter", "spring", "summer", "fall")))
bikeshare2
#   season
# 1 winter
# 2 spring
# 3 summer
# 4   fall

答案 1 :(得分:0)

要创建因子,您需要向factor()函数提供数据和季节标签。

但是,由于您希望将Spring用作基准级别,因此我相信您必须按特定顺序指定级别和标签-强制将Spring首先设置:

bikeshare <- data.frame(season = 1:4)

bikeshare$seasonfactor <- factor(x = bikeshare$season,
                                 levels = c(2,3,4,1), 
                                 labels = c("spring", "summer", "fall", "winter"))

str(bikeshare$seasonfactor)
#>  Factor w/ 4 levels "spring","summer",..: 4 1 2 3

bikeshare
#>   season seasonfactor
#> 1      1       winter
#> 2      2       spring
#> 3      3       summer
#> 4      4         fall

reprex package(v0.2.1)于2019-03-03创建

这有点令人困惑,因为在seasonfactor中,Spring用1表示,但是在原始season中它用2表示。

答案 2 :(得分:0)

另一种替代方法是在labelled包的帮助下标记整数级别。因此它们仍然是整数,但带有标签作为元数据。如果您想随时将带标签的整数转换为因数,可以使用to_factor函数来实现。

library("tidyverse")
library("labelled")

labels <- c(
  "winter" = 1,
  "spring" = 2,
  "summer" = 3,
  "fall" = 4)

bikeshare <-
  tibble(season = 1:4) %>%
  mutate(season = labelled(season, labels)) %>%
  mutate(seasonF = to_factor(season))
bikeshare
#> # A tibble: 4 x 2
#>       season seasonF
#>    <int+lbl> <fct>  
#> 1 1 [winter] winter 
#> 2 2 [spring] spring 
#> 3 3 [summer] summer 
#> 4 4 [fall]   fall

reprex package(v0.2.1)于2019-03-03创建