因数:int to factor

时间:2018-12-23 07:47:01

标签: r

我想将int转换为factor并使用适当的季节名称。下面的代码完成了该操作,但是当我使用glimpse()检查数据时,我的df仍将其显示为int。是因为我使用as.factor()而不是factor()吗?

day %>%
  as.tibble()%>%
  mutate(season = as.factor(season)%>%
           fct_recode("Spring" = "1", "Summer" = "2", "Fall" = "3", "Winter" = "4"))

glimpse(day)

2 个答案:

答案 0 :(得分:2)

您需要保存您的操作。那就是:

newday<-day %>%
  as.tibble()%>%
  mutate(season = as.factor(season)%>%
           fct_recode("Spring" = "1", "Summer" = "2", "Fall" = "3", "Winter" = "4"))

然后检查str(newday)

答案 1 :(得分:2)

我们可以使用return { foo=function() print("Hello World!") end } (化合物赋值运算符)进行更改

%<>%