我正在尝试确定我的每个列记录值的天数。它们都在不同的时间开始/停止记录,重要的是计算的总天数不包括列具有NA的时间。这是我的数据帧的一个例子
df = structure(list(Date_Time_GMT_3 = structure(c(1594233000, 1594533900, 1597235700,
1595234800, 1594336600, 1595237500),
class = c("POSIXct", "POSIXt"), tzone = "EST"),
`20874285_33MR` = c(14.996, 15.091, 15.187, 15.282, 15.378, 15.378),
`20874290_103MR` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
`20874287_102MR` = c(NA_real_, 15.091, 15.187, 15.282, NA_real_, NA_real_),
`20874299_54MR` = c(NA_real_, 15.378, 15.378, NA_real_, NA_real_, NA_real_),
`20874316_AIR_90MR` = c(NA_real_, NA_real_, NA_real_,15.091, 15.187, 15.282)),
row.names = c(NA, 6L), class = "data.frame")
时间并不重要。只要那天有记录,我就可以把它算作有1天记录的列。
最终结果应具有每列的总天数
这就是您想要做的吗?
library(dplyr)
df %>%
group_by(date = as.Date(Date_Time_GMT_3)) %>%
summarise(across(everything(), ~any(!is.na(.)))) %>%
summarise(across(-date, sum))
#> # A tibble: 1 x 6
#> Date_Time_GMT_3 `20874285_33MR` `20874290_103MR` `20874287_102MR` `20874299_54MR` `20874316_AIR_90MR`
#> <int> <int> <int> <int> <int> <int>
#> 1 5 5 0 3 2 2