R 重编码月份系数
我有这样一个数据集:R 重编码月份系数,r,refactoring,recode,R,Refactoring,Recode,我有这样一个数据集: age month 5 apr 6 jun 7 dec 当我写作时: str(data$month) 我有 Factor w/ 10 levels "apr","aug","dec",..: 7 7 7 7 7 7 7 7 7 7 ... 及 我想将因子重新编码为真实月份。我试过: month<-c("Jan","Feb","Mar", "Apr","May","Jun", "Jul","Aug","
age month
5 apr
6 jun
7 dec
当我写作时:
str(data$month)
我有
Factor w/ 10 levels "apr","aug","dec",..: 7 7 7 7 7 7 7 7 7 7 ...
及
我想将因子重新编码为真实月份。我试过:
month<-c("Jan","Feb","Mar",
"Apr","May","Jun",
"Jul","Aug","Sep",
"Oct","Nov","Dec")
data$month<-month[data$month]
data$month<-factor(data$month,levels=month.abb)
month似乎您只需要将第一个字母转换为大写字母。在stringr
包中有一个函数str_to_title
,可以实现这一点,即
library(stringr)
str_to_title(c('may', 'jun', 'jul'))
#[1] "May" "Jun" "Jul"
似乎您只需要将第一个字母转换为大写字母。在stringr
包中有一个函数str_to_title
,可以实现这一点,即
library(stringr)
str_to_title(c('may', 'jun', 'jul'))
#[1] "May" "Jun" "Jul"
library(stringr)
str_to_title(c('may', 'jun', 'jul'))
#[1] "May" "Jun" "Jul"