简体   繁体   English

当日期和时间在 R 的同一列中时如何读取 excel 文件

[英]How to read in excel file when Date and Time in the same column in R

I am trying to read an excel file into R.我正在尝试将 excel 文件读入 R。 Among other fields, the excel file has two "date" fields, each containing both the date and time stamp in the SAME field.在其他字段中,excel 文件有两个“日期”字段,每个字段都包含相同字段中的日期和时间戳。

Example:例子:

StartDate 9/14/2019 10:18:59 AM
EndDate 9/18/2019 2:27:14 AM

When I tried read_excel to read in the excel file, the data frame formatted these two columns very strangely.当我尝试用 read_excel 读取 excel 文件时,数据框对这两列进行了非常奇怪的格式化。 It spat out the days (with decimals).它吐出天数(带小数)。 Such as 43712.429849537039, Which I thought was days from Jan-01-1970 (the origin date that popped up when I typed lubrudate::origin).例如 43712.429849537039,我认为是从 Jan-01-1970 的几天(当我输入 lubrudate::origin 时弹出的原始日期)。

data %<>%    
  mutate(StartDate = as.Date(StartDate, origin = "1970-01-01 UTC"))

So I tried converting this back using as.Date, but it converts it to the totally wrong date... (converts all the dates to the year 2089).所以我尝试使用 as.Date 将其转换回来,但它会将其转换为完全错误的日期......(将所有日期转换为 2089 年)。 Example, 2089-09-05.例如,2089-09-05。

Any help with this would be really appreciated?对此有任何帮助将不胜感激? There must be a simpler way to directly read in a date-time column?!必须有一种更简单的方法可以直接读取日期时间列?!

You can use the lubridate package, it is excellent:您可以使用润滑 package,它非常好:

library(tidyverse)

df  <- data.frame(StartDate  =c("9/14/2019 10:18:59 AM","9/14/2019 3:18:59 PM"), 
                  EndDate= c("9/18/2019 2:27:14 AM","9/18/2019 1:27:14 PM"))


df <- df %>% mutate(StartDate = lubridate::mdy_hms(StartDate), EndDate = lubridate::mdy_hms(EndDate))

It turns out that excel has a different "origin date" from R.事实证明,excel 与 R 的“起源日期”不同。 Excels counts the days from 01-01-1900, where as R counts days from 01-01-1970. Excels 从 1900 年 1 月 1 日开始计算天数,而 R 从 1970 年 1 月 1 日开始计算天数。

When I used read_excel to read the file into a df, R used excels' counts of days.当我使用 read_excel 将文件读入 df 时,R 使用了 excels 的天数。 Which is why I got a weird date when I tried to convert to the date format using 1970. As soon as I used as.Date with excels "origin" date of 1990 (excels origin date), my dates parsed out correctly!这就是为什么当我尝试使用 1970 年转换为日期格式时,我得到一个奇怪的日期。只要我使用 as.Date 和 1990 年的“原始”日期(excel 原始日期),我的日期就正确解析了!

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

相关问题 R 读取 excel(xlsx) 文件日期、时间格式问题 - R read excel(xlsx) file date,time format issue 如何将带有日期列的time.series对象写入R中的Excel文件? - How to write time.series object with date column to an excel file in R? 如何将格式不明确的 excel 工作表中的日期列读入 R - How to read in a date column from an excel sheet with ambiguous formats into R 如何将日期和时间转换为 R 中 excel 中的持续时间列 - How to convert date and time into a duration column in excel in R 使用多种日期格式读取R中的Excel文件 - Read Excel file in R with multiple date format 读取在 r 列中具有 = 的 excel 文件 - read excel file which has = in a column in r 如何从R中的csv文件读取一列时间以进行数学运算? - How to read a column of time from a csv file in R for mathematical operations? 从 excel 到 r 读取日期列时设置自己的年月 - Set own year and month when read date column from excel to r 将多个 excel 文件导入 R 中的一个 df,同时向每个单独的文件添加一个新列 - Importing multiple excel files into one df in R whilst adding a new column to each individual file at the same time 在R中:使用posixct作为日期时间列时,如何消除日期时间序列图中的空白? - In R: how to remove gaps in date-time series plot when using posixct for date-time column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM