更新时间:2023-01-25 09:00:50
我们需要 na .rm = FALSE
。默认情况下,它是 na.rm = TRUE
,它会在开始时删除 NA
值,以便输出具有比原始数据集中的元素数量少。 dplyr :: mutate
仅在输出元素的长度与原始数据集的 length
相同时有效。
We need the na.rm=FALSE
. By default, it is na.rm=TRUE
which removes the NA
values at the beginning so that the output will have less number of elements than in the original dataset. The dplyr::mutate
will only work when the output elements have the same length
as the original dataset.
library(zoo)
library(dplyr)
sample %>%
group_by(ID) %>%
mutate(Date = na.locf(as.Date(Date, origin = '1970-01-01'), na.rm=FALSE))
# ID Date
# (int) (date)
#1 1 <NA>
#2 1 <NA>
#3 1 2015-01-01
#4 1 2015-01-01
#5 2 <NA>
#6 2 2015-01-01
#7 2 2015-01-01
#8 2 2015-01-01