我正在使用时间序列数据集,其中我有几个月的mmm(例如jan)格式我想将它们转换为mm(例如01)。 我的数据看起来像
X y
jan 1986 45
feb 1986 60
data %>%
tbl_df() %>%
select(-X1) %>%
separate(X,c("months","years")) %>%
mutate(months=replace(months,months=="fab","feb")) %>%
mutate(months =format(months,"%m",justify = "left",trim=TRUE))
当我运行上述命令时,我收到错误。
`Error in eval(substitute(expr), envir, enclos) :
invalid 'digits' argument
In addition: Warning message:
In format.default(c("jan", "feb", "mar", "apr", "may", "jun", "july", :
NAs introduced by coercion`
我正在使用dplyr
和zoo
包。
答案 0 :(得分:2)
这就是你要找的东西吗?
library(zoo)
as.Date(as.yearmon(df$x), format = "%b %Y")
# [1] "1986-01-01" "2001-02-01"
# OR
month(as.yearmon(df$x)) # from data.table package
# [1] 1 2
这种方法可能需要有干净的数据。这绝对是更好的方式。
months_list <- c("jan" = 01, "feb" = 02, "mar" = 03, "apr" = 04, "may" = 05,
"jun" = 06, "jul" = 07, "aug" = 08, "sep" = 09, "oct" = 10,
"nov" = 11, "dec" = 12)
df %>%
select(x) %>%
separate(x,c("months","years")) %>%
mutate(months=replace(months,months=="fab","feb")) %>%
mutate(months = months_list[months])
# months years
#1 2 1986
#2 12 2001
答案 1 :(得分:1)
是另一种以优化方式解决此问题的方法是
l<-"01"
d<-data %>%
tbl_df() %>%
select(-X1) %>%
separate(X,c("month","year")) %>%
mutate(month=replace(month,month=="fab","feb")) %>%
unite(date,year,month,sep="-") %>%
mutate(date=paste(date,l,sep="-")) %>%
mutate(date=ymd(date))
我在这里使用过lubridate包。