R 合并不同行中的日期和时间
我的“开始日期”列如下所示。其中一些既有日期又有时间,而一些只有日期或时间,而其余的是NA的 时间和小时应该与日期在同一行中,而时间应该是前一行中的日期和时间R 合并不同行中的日期和时间,r,R,我的“开始日期”列如下所示。其中一些既有日期又有时间,而一些只有日期或时间,而其余的是NA的 时间和小时应该与日期在同一行中,而时间应该是前一行中的日期和时间 Start.Date Values 11/6/2017\n07:00 a 3/22/2018\n06:38 b 11/6/2017 c 07:00 d <NA>
Start.Date Values
11/6/2017\n07:00 a
3/22/2018\n06:38 b
11/6/2017 c
07:00 d
<NA> e
<NA> f
<NA> g
<NA> h
11/5/2017\n07:00 i
3/21/2018\n06:38 j
有什么办法可以这样做吗?我尝试使用tidyr提取它,但没有得到预期的结果。谢谢大家! 首先尝试使用
mdy\u hm
函数从lubridate
转换Start.Date
。然后使用tidyr::fill
更新不包含有效日期/时间的行(即NA
)
注意:上述答案与OP的预期不符。可能需要正确排列行
数据:
df <- read.table(text =
"Start.Date Values
'11/6/2017\n07:00' a
'3/22/2018\n06:38' b
11/6/2017 c
07:00 d
NA e
NA f
NA g
NA h
'11/5/2017\n07:00' i
'3/21/2018\n06:38' j",
header = TRUE, stringsAsFactors = FALSE)
df为什么3rd
行开始。日期是2017年11月6日\n07:00
而不是2018年3月22日\n06:38
?是否有机会在更接近源代码/读取时修复此问题?那些\n
换行符看起来不太好。@MKR日期安排不正确,我需要对其进行标准化,以便使用lubridate()。@AkselA,我无法将其修复得离源代码更近,因为我使用scraper函数循环了数百个此类文档。@Tze那么即使是第二行也应该有值为11/6/2017\n07:00
。我说的对吗?它与预期的输出不匹配,但感谢您的帮助!我将尝试手动清理它。
library(dplyr)
library(tidyr)
library(lubridate)
df %>% mutate(Start.Date = mdy_hm(Start.Date)) %>%
fill(Start.Date)
# Start.Date Values
# 1 2017-11-06 07:00:00 a
# 2 2018-03-22 06:38:00 b
# 3 2018-03-22 06:38:00 c
# 4 2018-03-22 06:38:00 d
# 5 2018-03-22 06:38:00 e
# 6 2018-03-22 06:38:00 f
# 7 2018-03-22 06:38:00 g
# 8 2018-03-22 06:38:00 h
# 9 2017-11-05 07:00:00 i
# 10 2018-03-21 06:38:00 j
df <- read.table(text =
"Start.Date Values
'11/6/2017\n07:00' a
'3/22/2018\n06:38' b
11/6/2017 c
07:00 d
NA e
NA f
NA g
NA h
'11/5/2017\n07:00' i
'3/21/2018\n06:38' j",
header = TRUE, stringsAsFactors = FALSE)