简体   繁体   English

在R Studio中使用Lubridate创建年,月,日列会产生意外的结果

[英]Using Lubridate in R Studio to create year, month, day columns gives unexpected results

I am importing a csv file with dates in and using lubridate to create additional columns to show the year, month and day. 我正在导入日期为csv的文件,并使用lubridate创建其他列以显示年,月和日。 The date column in the csv file is called "Date". CSV文件中的日期列称为“日期”。 My code is as below: 我的代码如下:

library (lubridate)
Dates<-  read.csv("DateSpreadsheet.csv")
Dates$Year<- year(Dates$Date)
Dates$Month<- month(Dates$Date)
Dates$Day<-day(Dates$Date)
View (Dates)

The problem is that in the table produced the year column shows the day and the day column shows the first 2 digits of the year. 问题在于,在产生的表中,年列显示日期,而天列显示年份的前两位数字。

Table showing columns for date, year, month and day 该表显示日期,年,月和日的列

I would be grateful for any advice. 如有任何建议,我将不胜感激。

This is happening because the date is not stored in the ymd format. 发生这种情况是因为日期未以ymd格式存储。 You can use the following code yo update your date table and it will give you the correct output. 您可以使用以下代码来更新日期表,它将为您提供正确的输出。

Dates$Date<-dmy(Dates$Date)

let me know if this works. 让我知道这个是否奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM