在数据框中生成Date列

时间:2018-02-17 20:11:34

标签: r date

我有下表的季度数据,并希望为每一行生成一个新的日期类型列。

Year,Quarter,Sales
2008,1,1.703
2008,2,0.717
2008,3,6.892
2008,4,4.363
2009,1,3.793
2009,2,5.208
2009,3,7.367
2009,4,8.737
2010,1,8.752
2010,2,8.398

这就是我试过的

quarters <- c('-03-31', '-06-30', '-09-30', '-12-31')
gen_date <- function(row) {
    year <- row[1]
    quarter <- row[2]
    date <- paste(toString(year), quarters[quarter], sep='')
    date <- as.Date((date), format="%Y-%m-%d")
    return(date)
}

df$Date <- apply(df, 1, gen_date)

但是,结果列df $ Date不是日期,而是int。

   Year Quarter       Sales  Date
1  2008       1        1.703 13969
2  2008       2        0.717 14060
3  2008       3        6.892 14152
4  2008       4        4.363 14244
5  2009       1        3.793 14334
6  2009       2        5.208 14425
7  2009       3        7.367 14517
8  2009       4        8.737 14609

2 个答案:

答案 0 :(得分:1)

尝试使用lubridate:

library(lubridate)
Year=c(rep(2008,4),rep(2009,4),2010,2010)
Quarter=c(1,2,3,4,1,2,3,4,1,2)
Sales=c(1.7,0.7,6.9,4.3,3.79,5.2,7.3,8.7,8.7,8.4)
df=tibble(Year,Quarter,Sales)
df$Date=yq(paste(as.character(df$Year),as.character(df$Quarter),sep="-"))
df



Year Quarter Sales Date      
   <dbl>   <dbl> <dbl> <date>    
 1  2008    1.00 1.70  2008-01-01
 2  2008    2.00 0.700 2008-04-01
 3  2008    3.00 6.90  2008-07-01
 4  2008    4.00 4.30  2008-10-01
 5  2009    1.00 3.79  2009-01-01
 6  2009    2.00 5.20  2009-04-01
 7  2009    3.00 7.30  2009-07-01
 8  2009    4.00 8.70  2009-10-01
 9  2010    1.00 8.70  2010-01-01
10  2010    2.00 8.40  2010-04-01

答案 1 :(得分:0)

试试这个:

库(lubridate)

dfx <- read.table(text = "Year,Quarter,Sales
2008,1,1.703
2008,2,0.717
2008,3,6.892
2008,4,4.363
2009,1,3.793
2009,2,5.208
2009,3,7.367
2009,4,8.737
2010,1,8.752
2010,2,8.398", header=T, sep=",")

dfx$month <- factor(dfx$Quarter)
levels(dfx$month) <- c('-03-31', '-06-30', '-09-30', '-12-31')
dfx$month <- as.character(dfx$month)

dfx$date <- ymd(paste(dfx$Year, dfx$month, sep="-"))

HTH