问题描述
这是一个例子。我需要提取最短观察日期,以便将表格扩展回开始日期。它正在工作,现在不是:在原点 1970-01-10 附近生成一个整数或日期?我尝试使用 as.Date() 转换为数字并返回,并将 na 函数更改为 na.omit 并阅读了大量帖子。我相信这很简单,但它已经打败了我。
library(tidyr)
library(lubridate)
tt <- tibble(month = factor(rep("Mar",15)),treatment = factor(rep(c("a","b","c"),each = 5)),observation = c("","","05-04-2019","24-04-2019","27-04-2019","28-04-2019","30-04-2019","05-05-2019","09-05-2019","12-05-2019","20-05-2019",""))
ts <- tt %>% mutate(observation = dmy(observation)) %>%
group_by(treatment) %>%
summarise(first = min(!is.na(observation)))
ts
# A tibble: 3 x 2
treatment first
* <fct> <int>
1 a 0
2 b 0
3 c 0
解决方法
好的,问题在于缺失值。删除它们就可以了:-)
summarise(first = min(na.omit(observation)))
替换
summarise(first = min(!is.na(observation)))
玛雅克