我在以下方面遇到了问题:我想将日期格式从“%d%/ m /%y”更改为“%y%/ m /%d”,然后创建一个仅包含YEAR(从列日期中提取年份)。
data$date_of_purchase = as.Date(data$date_of_purchase, "%y%/m/%d")
data$year_of_purchase = as.numeric(format(data$date_of_purchase, "%Y"))
例如:08/01/2019将更改为2008-01-20,但应为2019/01/08。
# Load text file into local variable called 'data'
data = read.delim(file = 'purchases.txt', header = FALSE, sep = '\t', dec = '.')
# Add headers and interpret the last column as a date, extract year of purchase
colnames(data) = c('customer_id', 'purchase_amount', 'date_of_purchase')
#Delete columns with Blank Values
data <- with(data, data[!(customer_id == "" | is.na(customer_id)), ])
data$date_of_purchase = as.Date(data$date_of_purchase, "%y/%m/%d")
data$year_of_purchase = as.numeric(format(data$date_of_purchase, "%Y"))
没有错误消息,但是新日期不是所需的日期。
答案 0 :(得分:0)
这是您要寻找的吗?
library("lubridate") ## year comes from this package
Input
data <- fread("customer_id purchase_amount date_of_purchase
0000@000.com $22 08/01/2019
0000christie@gmail.com $174 02/11/2017
0000christie@gmail.com $61 03/01/2018
000@hotmail.com $33 16/12/2017")
Your code
data$date_of_purchase = as.Date(data$date_of_purchase,"%d/%m/%Y")
data$year_of_purchase <- year(data$date_of_purchase)
OutPut:
customer_id purchase_amount date_of_purchase year_of_purchase
1: 0000@000.com $22 2019-01-08 2019
2: 0000christie@gmail.com $174 2017-11-02 2017
3: 0000christie@gmail.com $61 2018-01-03 2018
4: 000@hotmail.com $33 2017-12-16 2017
答案 1 :(得分:0)
X $ newdate <-strptime(as.character(X $ date),“%d /%m /%Y”)
format(X $ newdate,“%Y-%m-%d”)