> df<-read.csv("weatherdatatrail.csv")
> header=T
> df$Date
[1] 01-01-1989 01-02-1989 01-03-1989 01-04-1989 01-05-1989 01-06-1989
[7] 01-07-1989 01-08-1989 01-09-1989 01-10-1989 01-11-1989 01-12-1989
[13] 1/13/1989 1/14/1989 1/15/1989 1/16/1989 1/17/1989 1/18/1989
[19] 1/19/1989 1/20/1989 1/21/1989 1/22/1989 1/23/1989 1/24/1989
[25] 1/25/1989 1/26/1989 1/27/1989 1/28/1989 1/29/1989 1/30/1989
[31] 1/31/1989
31 Levels: 01-01-1989 01-02-1989 01-03-1989 01-04-1989 ... 1/31/1989
在为以下代码执行R时,它适用于通用格式,但对于日期格式,它显示为NA ..
> df$Date<-as.Date(as.character(df$Date),format="%m/%d/%Y")
> str(df)
'data.frame': 31 obs. of 10 variables:
$ Date : Date, format: NA NA ...
$ Longitude : num 77.2 77.2 77.2 77.2 77.2 ...
$ Latitude : num 11.4 11.4 11.4 11.4 11.4 ...
$ Elevation : int 282 282 282 282 282 282 282 282 282 282 ...
$ Max.Temperature : num 28.2 28.5 29.6 29.3 27.9 ...
$ Min.Temperature : num 10.9 15.4 14.2 16.5 16.3 ...
$ Precipitation : num 0 0.0378 0.0893 0.2077 0.1785 ...
$ Wind : num 2.17 2.1 2.03 2.05 2.09 ...
$ Relative.Humidity: num 0.559 0.705 0.718 0.779 0.781 ...
$ Solar : num 21 20.5 19.3 19.2 14.8 ...
> df$Date
[1] NA NA NA NA NA
[6] NA NA NA NA NA
[11] NA NA "1989-01-13" "1989-01-14" "1989-01-15"
[16] "1989-01-16" "1989-01-17" "1989-01-18" "1989-01-19" "1989-01-20"
[21] "1989-01-21" "1989-01-22" "1989-01-23" "1989-01-24" "1989-01-25"
[26] "1989-01-26" "1989-01-27" "1989-01-28" "1989-01-29" "1989-01-30"
[31] "1989-01-31"
我无法取得完美的结果..请帮帮我
答案 0 :(得分:1)
您可以使用library(lubridate)
(由@MrFlick评论)
library(lubridate)
as.Date(mdy(v1))
#[1] "1989-01-01" "1989-01-02" "1989-01-15"
v1 <- c("01-01-1989", "01-02-1989", "1/15/1989")
答案 1 :(得分:1)
您可以将日期列更改为相同的格式,然后将其转换为日期类型
dates <- c("01-01-1989" , "01-02-1989" , "01-03-1989" , "01-04-1989" , "01-05-1989" , "01-06-1989" ,
"01-07-1989" , "01-08-1989" , "01-09-1989" , "01-10-1989" , "01-11-1989" ,"01-12-1989" ,
"1/13/1989" , "1/14/1989" , "1/15/1989" ,"1/16/1989" , "1/17/1989", "1/18/1989" ,
"1/1/1989")
df <- data.frame(Date = dates)
df$Date <- gsub(df$Date , pattern = "-" , replacement = "/")
df$Date <- as.Date(df$Date,format="%m/%d/%Y")
答案 2 :(得分:0)
> as.Date(parse_date_time(df$Date,c("%m/%d/%Y")))
[1] "1989-01-01" "1989-01-02" "1989-01-03" "1989-01-04" "1989-01-05"
[6] "1989-01-06" "1989-01-07" "1989-01-08" "1989-01-09" "1989-01-10"
[11] "1989-01-11" "1989-01-12" "1989-01-13" "1989-01-14" "1989-01-15"
[16] "1989-01-16" "1989-01-17" "1989-01-18" "1989-01-19" "1989-01-20"
[21] "1989-01-21" "1989-01-22" "1989-01-23" "1989-01-24" "1989-01-25"
[26] "1989-01-26" "1989-01-27" "1989-01-28" "1989-01-29" "1989-01-30"
[31] "1989-01-31"