我无法将列更改为R中的日期格式

时间:2017-07-11 19:52:07

标签: r date datetime

This is how my data looks

这是我的数据帧的字符串。

'data.frame':   10652 obs. of  4 variables:
 $ Date: chr  "06-15-2017" "06-15-2017" "06-15-2017" "06-15-2017" ...
 $ Time: Factor w/ 951 levels "00:00:01","00:00:02",..: 396 398 400 402 404 406 407 409 411 413 ...
 $ CPU : num  2.4 2.4 2.3 2.3 2.2 2.2 2.1 2.1 2.1 2.1 ...
 $ MEM : num  2.5 2.5 2.5 2.6 2.6 2.6 2.6 2.7 2.9 2.9 ...

我想让R以日期和时间格式读取日期和时间列。

我试过了:

  1. DateData$Date_Time = within(DateData, { timestamp=format(as.POSIXct(paste(DateData$Date, DateData$Time)), "%d/%m/%Y %H:%M:%S") })

  2. 我在合并日期和时间栏后尝试了这一点 -
    DateData$Date_Time = as.chron(DateData$Date_Time, "%d/%m/%Y %H:%M:%S")

  3. DateData = within(DateData, { timestamp=strptime(paste((DateData$Date, DateData$Time), "%Y/%m/%d%H:%M:%S") })

  4. 而且:DateData$DateTime = strptime(DateData$DateTime,"%m-%d-%Y %H:%M:%S")

  5. 似乎没有什么对我有用。

3 个答案:

答案 0 :(得分:1)

导入数据后处理转换

这是您的数据样本

df <- data.frame(Date = c("06-15-2017","06-15-2017","06-15-2017","06-15-2017"), Time = c("00:00:01", "00:00:02", "00:00:03", "00:00:04"), stringsAsFactors = F)

对于日期对象,您可以使用基数R,lubridateanytime 封装

df$Date_base <- as.Date(df$Date, format = "%m-%d-%y")

library(lubridate)
#> 
#> Attachement du package : 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
df$Date_lubridate <- mdy(df$Date)

library(anytime)
df$Date_anytime <- anytime(df$Date) 

仅适用于时间对象(不是日期时间),您可以使用 hms包或period个对象与lubridate包形成 lubridate::hms

library(hms)
#> 
#> Attachement du package : 'hms'
#> The following object is masked from 'package:lubridate':
#> 
#>     hms
df$Time_hms <- as.hms(df$Time)
df$Time_lubridate <- lubridate::hms(df$Time) # hms in lubridate is masked by hms package

这里的结果是什么样的

df
#>         Date     Time  Date_base Date_lubridate Date_anytime Time_hms
#> 1 06-15-2017 00:00:01 2020-06-15     2017-06-15   2017-06-15 00:00:01
#> 2 06-15-2017 00:00:02 2020-06-15     2017-06-15   2017-06-15 00:00:02
#> 3 06-15-2017 00:00:03 2020-06-15     2017-06-15   2017-06-15 00:00:03
#> 4 06-15-2017 00:00:04 2020-06-15     2017-06-15   2017-06-15 00:00:04
#>   Time_lubridate
#> 1             1S
#> 2             2S
#> 3             3S
#> 4             4S

列的类和df的摘要

sapply(df, class)
#> $Date
#> [1] "character"
#> 
#> $Time
#> [1] "character"
#> 
#> $Date_base
#> [1] "Date"
#> 
#> $Date_lubridate
#> [1] "Date"
#> 
#> $Date_anytime
#> [1] "POSIXct" "POSIXt" 
#> 
#> $Time_hms
#> [1] "hms"      "difftime"
#> 
#> $Time_lubridate
#> [1] "Period"
#> attr(,"package")
#> [1] "lubridate"
summary(df)
#>      Date               Time             Date_base         
#>  Length:4           Length:4           Min.   :2020-06-15  
#>  Class :character   Class :character   1st Qu.:2020-06-15  
#>  Mode  :character   Mode  :character   Median :2020-06-15  
#>                                        Mean   :2020-06-15  
#>                                        3rd Qu.:2020-06-15  
#>                                        Max.   :2020-06-15  
#>  Date_lubridate        Date_anytime          Time_hms       
#>  Min.   :2017-06-15   Min.   :2017-06-15   Length:4         
#>  1st Qu.:2017-06-15   1st Qu.:2017-06-15   Class1:hms       
#>  Median :2017-06-15   Median :2017-06-15   Class2:difftime  
#>  Mean   :2017-06-15   Mean   :2017-06-15   Mode  :numeric   
#>  3rd Qu.:2017-06-15   3rd Qu.:2017-06-15                    
#>  Max.   :2017-06-15   Max.   :2017-06-15                    
#>  Time_lubridate 
#>  Min.   :1S     
#>  1st Qu.:1.75S  
#>  Median :2.5S   
#>  Mean   :2.5S   
#>  3rd Qu.:3.25S  
#>  Max.   :4S

在阅读时直接处理转换

使用readr包从文件中读取文件时,可以直接处理类型转换。

library(readr)
read_csv('Date, Time
06-15-2017, 00:00:01
06-15-2017, 00:00:02
06-15-2017, 00:00:03
06-15-2017, 00:00:04
', col_types = cols(Date = col_date(format = "%m-%d-%Y"), 
                    Time = col_time()))
#> # A tibble: 4 x 2
#>         Date     Time
#>       <date>   <time>
#> 1 2017-06-15 00:00:01
#> 2 2017-06-15 00:00:02
#> 3 2017-06-15 00:00:03
#> 4 2017-06-15 00:00:04

使用readr,您会看到它直接将数据导入data.frame(来自tibble的特殊tidyverse格式),其中列为日期和时间。您可以找到一些信息here

答案 1 :(得分:0)

您使用的日期时间格式与多个地方的数据不匹配。 如果您将DateTime列与空格分隔符粘贴在一起,则要解析的格式为%m-%d-%Y %H:%M:%S

即,将两列组合并解析为日期时间:

DateData$DateTime <- strptime(paste(DateData$Date, DateData$Time, sep=' '), '%m-%d-%Y %H:%M:%S')

答案 2 :(得分:0)

installing lubridate package

install.packages("lubridate") library (lubridate)

pasting the Date and Time Column

DFanalysis$DateStamp <- paste(DFanalysis$Date, DFanalysis$Time, sep = " ")

Check the class of DateStamp

class(DFanalysis$DateStamp)

If the class is character we can convert it directly

DFanalysis$DateStamp <- mdy_hms(DFanalysis$DateStamp)