我有一个来自此csv文件的日期,并希望将它们转换为R中的日期格式
BPL16_17 <- read.csv("BPL16:17.csv")
attach(BPL16_17)
newdate<-strptime(Date,format =("%Y-%m-%d"))
newdate
for newdate i get; [1] NA NA NA NA NA NA NA NA ...
答案 0 :(得分:0)
尝试使用stringsAsFactors=FALSE
中的read.csv
选项将Date列作为字符串读取而不是因素。
或者,使用as.character(BPL16_17$Date)
答案 1 :(得分:0)
示例中使用的数据集是来自here的flight-data
。
#read the dataset
df<-read.csv("D:/data/airline_delay_2014_1.csv", stringsAsFactors = FALSE)
str(df)
'data.frame': 471949 obs. of 27 variables:
$ YEAR : int 2014 2014 2014 2014 2014 2014 2014 2014 2014 2014 ...
$ MONTH : int 1 1 1 1 1 1 1 1 1 1 ...
$ DAY_OF_MONTH : int 25 30 24 1 3 4 5 9 13 12 ...
$ FL_DATE : chr "2014-01-25" "2014-01-30" "2014-01-24" "2014-01-01" ...
$ CARRIER : chr "EV" "EV" "EV" "EV" ...
$ TAIL_NUM : chr "N12530" "N15527" "N11526" "N28529" ...
$ FL_NUM : int 4524 4524 4524 4524 4524 4524 4524 4524 4524 4524 ...
$ ORIGIN : chr "LFT" "LFT" "LFT" "LFT" ...
$ ORIGIN_CITY_NAME : chr "Lafayette, LA" "Lafayette, LA" "Lafayette, LA" "Lafayette, LA" ...
$ ORIGIN_STATE_ABR : chr "LA" "LA" "LA" "LA" ...
$ ORIGIN_STATE_NM : chr "Louisiana" "Louisiana" "Louisiana" "Louisiana" ...
$ DEST_AIRPORT_ID : int 12266 12266 12266 12266 12266 12266 12266 12266 12266 12266 ...
$ DEST : chr "IAH" "IAH" "IAH" "IAH" ...
$ DEST_CITY_NAME : chr "Houston, TX" "Houston, TX" "Houston, TX" "Houston, TX" ...
$ DEST_STATE_ABR : chr "TX" "TX" "TX" "TX" ...
$ DEST_STATE_NM : chr "Texas" "Texas" "Texas" "Texas" ...
$ DEP_TIME : int NA NA NA 636 959 700 654 656 651 656 ...
$ DEP_DELAY : num NA NA NA -12 191 12 6 1 -9 1 ...
$ ARR_TIME : int NA NA NA 733 1057 811 757 804 749 800 ...
$ ARR_DELAY : num NA NA NA -23 181 15 1 2 -18 -2 ...
$ CANCELLED : num 1 1 1 0 0 0 0 0 0 0 ...
$ CANCELLATION_CODE: chr "B" "B" "B" "" ...
$ AIR_TIME : num NA NA NA 38 39 40 43 52 47 40 ...
$ DISTANCE : num 201 201 201 201 201 201 201 201 201 201 ...
$ CARRIER_DELAY : num NA NA NA NA 181 12 NA NA NA NA ...
$ WEATHER_DELAY : num NA NA NA NA 0 0 NA NA NA NA ...
$ X : logi NA NA NA NA NA NA ...
class(df$FL_DATE)
[1] "character"
将FL_DATE
列转换为Date
类,
df$FL_DATE<- as.Date(df$FL_DATE, format = "%Y-%m-%d")
#check class now
class(df$FL_DATE)
[1] "Date"