我有一列数据:
dates <- c("20140106_20140131", "20140106_20140331", "20140201_20140210",
"20140201_20140228", "20140211_20140220", "20140221_20140228",
"20140301_20140310", "20140301_20140331")
我希望R知道这些变量是“迄今为止的日期”格式
问题
答案 0 :(得分:0)
# vector of your date ranges
dates <- c("20140106_20140131", "20140106_20140331", "20140201_20140210",
"20140201_20140228", "20140211_20140220", "20140221_20140228",
"20140301_20140310", "20140301_20140331")
library('stringr')
library('lubridate')
首先创建一个包含两列日期的数据框。
date_frame <- data.frame(str_split_fixed(dates, "_", 2))
然后使用lubridate
&#39; ymd()
函数转换字符串中的日期。
date_frame$X1 <- ymd(date_frame$X1)
date_frame$X2 <- ymd(date_frame$X2)
创建一个新的时差列。
transform(date_frame, diff = X2 - X1)
X1 X2 diff
1 2014-01-06 2014-01-31 25 days
2 2014-01-06 2014-03-31 84 days
3 2014-02-01 2014-02-10 9 days
4 2014-02-01 2014-02-28 27 days
5 2014-02-11 2014-02-20 9 days
6 2014-02-21 2014-02-28 7 days
7 2014-03-01 2014-03-10 9 days
8 2014-03-01 2014-03-31 30 days