### The sample data
df <- data.frame(months = c('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'),
values = sample(100:200, 12, replace = TRUE))
### Load the library needed
library(dplyr)
我尝试了以下方法将三个字母的月份(例如jan)转换为日期:
df.1 <- mutate(df, year = 2015)
df.1 <- mutate(df.1, dates = sapply(months, function(x) grep(paste("(?i)", x, sep = ""), month.abb)))
df.1 <- mutate(df.1, dates1 = paste(dates, year, sep = "-"))
df.1 <- mutate(df.1, dates2 = as.Date(df.1$dates1, "%m-%Y"))
但是dates2
列产生了NA
,但是当我检查结构时,结果是:
> str(df.1)
'data.frame': 12 obs. of 6 variables:
$ months: Factor w/ 12 levels "apr","aug","dec",..: 5 4 8 1 9 7 6 2 12 11 ...
$ values: int 141 147 176 189 113 181 149 114 121 191 ...
$ year : num 2015 2015 2015 2015 2015 ...
$ dates : int 1 2 3 4 5 6 7 8 9 10 ...
$ dates1: chr "1-2015" "2-2015" "3-2015" "4-2015" ...
$ dates2: Date, format: NA NA NA NA ..
有没有办法将months
列转换为日期类?怎么样?谢谢你的帮助。
答案 0 :(得分:3)
据我所知,您不能使用as.Date
或任何类似的功能将月份转换为该月份的日期实体。您必须提供更多信息,例如一天,即
as.Date("26jan","%d%b")
[1] "2017-01-26"
或使用如下功能:
library(zoo)
as.yearmon("jan","%b")
[1] "Jan 2017"
所以对于你的表达式来说,虽然不确定代码的用途,但是在这里你会去:
df <- data.frame(months = c('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'),
values = sample(100:200, 12, replace = TRUE))
df.1 <- mutate(df, year = 2015)
df.1 <- mutate(df.1, dates = sapply(months, function(x) grep(paste("(?i)", x, sep = ""), month.abb)))
df.1 <- mutate(df.1, dates1 = paste(year,dates, sep = "-"))
df.1 <- mutate(df.1, dates2 = yearmon(df.1$dates1, "%Y-%m"))
as.yearmon(df.1$dates1)
[1] "Jan 2015" "Feb 2015" "Mar 2015" "Apr 2015" "May 2015" "Jun 2015" "Jul 2015" "Aug 2015" "Sep 2015" "Oct 2015"
[11] "Nov 2015" "Dec 2015"
答案 1 :(得分:-1)
感谢Nadizan's answer指出day
方面,所以我重新构建了我的语法,如下所示:
df.1 <- mutate(df, year = "1-2015")
df.1 <- mutate(df.1, dates = sapply(months, function(x) grep(paste("(?i)", x, sep = ""), month.abb)))
df.1 <- mutate(df.1, dates1 = paste(dates, year, sep = "-"))
df.1 <- mutate(df.1, dates2 = as.Date(df.1$dates1, "%m-%d-%Y"))
然后我检查了结构,结果是:
> str(df.1)
'data.frame': 12 obs. of 6 variables:
$ months: Factor w/ 12 levels "apr","aug","dec",..: 5 4 8 1 9 7 6 2 12 11 ...
$ values: int 110 154 200 110 105 179 139 173 153 197 ...
$ year : chr "1-2015" "1-2015" "1-2015" "1-2015" ...
$ dates : int 1 2 3 4 5 6 7 8 9 10 ...
$ dates1: chr "1-1-2015" "2-1-2015" "3-1-2015" "4-1-2015" ...
$ dates2: Date, format: "2015-01-01" "2015-02-01" "2015-03-01" "2015-04-01" ...