[英]How can I detect the earliest instance of a non-NA value?
I have a data frame which lists events against the dates they occurred.我有一个数据框,它根据发生的日期列出事件。
How can I record the first date a non- NA
value appears in the dataset?如何记录非NA
值出现在数据集中的第一个日期?
For example, given this dataset:例如,给定这个数据集:
Colour Date
<chr> <date>
Blue 2021-06-29
NA 2021-03-10
NA 2021-04-02
Amber 2021-04-09
Blue 2021-06-21
NA 2021-03-09
Blue 2021-04-11
Amber 2021-05-21
NA 2021-02-17
How can I find the first/earliest time a non- NA
value appears in terms of date/history?如何根据日期/历史找到非NA
值出现的第一次/最早时间?
In this example, the result would be Amber, 2021-04-09
as the values that occur before it in time are all NA
up until this point.在此示例中,结果将是Amber, 2021-04-09
因为在它之前出现的值在此之前都是NA
。
If we need the first non-NA by 'Date', arrange
first and then slice
the first row如果我们需要'Date'的第一个非NA,首先arrange
然后slice
第一行
library(dplyr)
df1 %>%
arrange(is.na(Colour), Date) %>%
slice_head(n = 1)
-output -输出
Colour Date
1 Amber 2021-04-09
df1 <- structure(list(Colour = c("Blue", NA, "Amber", "Blue", NA, "Blue",
"Amber", NA), Date = structure(c(18807, 18696, 18726, 18799,
18695, 18728, 18768, 18675), class = "Date")), row.names = c(NA,
-8L), class = "data.frame")
I guess you can try the code below我想你可以试试下面的代码
> subset(df[order(df$Date), ], min(which(!is.na(Colour))) == seq_along(Colour))
Colour Date
3 Amber 2021-04-09
Order the date, drop NA
values and select the first row.对日期进行排序,删除NA
值并选择第一行。
library(dplyr)
df1 %>% arrange(Date) %>% na.omit() %>% slice(1L)
# Colour Date
#1 Amber 2021-04-09
In base R -在基础 R -
na.omit(df1[order(df1$Date), ])[1, ]
Without using any library:不使用任何库:
df1 = df[order(df$Date),] # orders by date
df1[which(!is.na(df1$Colour))[1],] # pick the first non-empty colour
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.