简体   繁体   English

如果我的月、日和年在不同的列中,我可以使用 R 包 lubridate 来解析日期吗?

[英]Can I use the R package lubridate to parse dates if my month, day, and year are in separate columns?

I am new at using R, but I am trying to learn how to use it to make my data analyses more reproducible.我是 R 的新手,但我正在尝试学习如何使用它来使我的数据分析更具可重复性。 I have my dates entered in three columns for the drop off date and three columns for the pickup date (one for month, one for day, and one for year).我将日期输入到三列作为还车日期和三列作为取件日期(一列表示月,一列表示日,一列表示年)。 I need to be able to get R to recognize it as a date, so I can calculate the time in field as a fraction of a year (days/365).我需要能够让 R 将其识别为日期,因此我可以将现场时间计算为一年的一小部分(天/365)。 I installed the lubridate package and tried using the mdy() function, but it gave me the following error message:我安装了 lubridate 包并尝试使用 mdy() 函数,但它给了我以下错误消息:

Error: Column `drop_off_date` must be length 150 (the number of rows) or one, not 450
In addition: Warning message:
All formats failed to parse. No formats found. 

I also tried using backticks, but that did not work either.我也尝试使用反引号,但这也不起作用。 I think it may be because of how my dates are set up in different columns, but I am not sure.我想这可能是因为我的日期在不同的列中是如何设置的,但我不确定。 This is the section of code I used for that:这是我用于此的代码部分:

mutate(drop_off_date = mdy(dropoff_month, dropoff_day, dropoff_year),
         pickup_date = mdy(pickup_month, pickup_day, pickup_year),

Does anyone have any suggestions for a different function or what I could fix to use this function?有没有人对不同的功能有任何建议,或者我可以修复什么来使用这个功能?

The lubridate functions take a single vector of strings. lubridate函数采用单个字符串向量。 My first comment suggested paste0 could work too, but not directly (see the code below), so you need to include a sep arator (such as paste 's default " " space).我的第一个评论所说paste0可以工作过,但不是直接(见下面的代码),所以你需要包括sep arator(如paste的默认" "空间)。

library(lubridate)
### wrong
mdy(10, 13, 2018)
# Warning: All formats failed to parse. No formats found.
# [1] NA NA NA

### some fixed
mdy(paste(10, 13, 2018))
# [1] "2018-10-13"

library(dplyr)
data.frame(y=c(2018,2019), m=c(10,9), d=c(30,1)) %>%
  mutate(date = mdy(paste(m, d, y)))
#      y  m  d       date
# 1 2018 10 30 2018-10-30
# 2 2019  9  1 2019-09-01

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

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