[英]Using tidyr::fill() to fill in blank dates
尝试使用tidyr::fill()
填充测试数据集中inception_date
和expiry_date
字段的空白(向下方向填充):
遇到麻烦。 例如,如果我运行:
library(dplyr)
library(tidyr)
test <- test %>%
mutate(new_inception_date = fill(inception_date, .direction = "down"))
我收到一条错误消息
no applicable method for 'fill' applied to an object of class "Date"
fill()
不适用于日期字段吗? 有解决方法吗?
谢谢。
正如建议的那样, fill
一次在一个框架上工作。 来自?fill
:
Usage:
fill(data, ..., .direction = c("down", "up", "downup", "updown"))
Arguments:
data: A data frame.
...: <'tidy-select'> Columns to fill.
.direction: Direction in which to fill missing values. Currently either
"down" (the default), "up", "downup" (i.e. first down and
then up) or "updown" (first up and then down).
其用法的快速演示:
df <- data.frame(date = Sys.Date() + replace(1:8, c(1, 4, 7), NA))
df
# date
# 1 <NA>
# 2 2023-02-04
# 3 2023-02-05
# 4 <NA>
# 5 2023-02-07
# 6 2023-02-08
# 7 <NA>
# 8 2023-02-10
df %>%
fill(date)
# date
# 1 <NA>
# 2 2023-02-04
# 3 2023-02-05
# 4 2023-02-05
# 5 2023-02-07
# 6 2023-02-08
# 7 2023-02-08
# 8 2023-02-10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.