如何计算每列在R中记录值的总天数

How to calculate total number of days each column recorded a value in R

我正在尝试确定我的每个列记录值的天数。他们都 start/stop 在不同的时间记录,重要的是计算的总天数不包括列有 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