简体   繁体   English

如何对 R 中的 data.frame 或 tibble 列中的 NA 值的总数求和,并按月和年对它们进行分组

[英]How can I sum the totals of NA values in a data.frame or tibble column in R and group them by Month and Year

So here is a sample of my Data所以这是我的数据样本

year <- c(1980  ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,
      1980  ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,
      1980  ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,
      1980  ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,
      1980  ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,
      1980  ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980   ,1980)

month <- c(1    ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1
       ,1   ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,2  
       ,2   ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  
       ,2   ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2  ,2)

Q <- c(NA   ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA 
   ,NA  ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,0.3    ,0.3    ,0.28   
   ,0.26    ,0.26   ,0.25   ,0.25   ,0.24   ,0.24   ,0.24   ,0.24   ,0.23   ,0.23   ,NA ,NA 
   ,NA  ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA ,NA)

I combined them into a dataframe called Flow我将它们组合成一个名为 Flow 的 dataframe

Flow <- data.frame(year,month,Q)

I can sum or count the number of missing or NA values in my Q column.我可以总结或计算我的 Q 列中缺失或 NA 值的数量。

sum(is.na(Flow$Q))

Now I am trying to calculate the sum of NA values in each month for the year and eventually each year.现在我正在尝试计算一年中每个月以及最终每年的 NA 值的总和。

This is where I'm stuck.这就是我卡住的地方。

group_by(Flow$year, Flow$month) %>%
sum(is.na(Flow$Q) 

With group by, we can use summarise .使用 group by,我们可以使用summarise Also, we don't need the Flow$ inside the group_by此外,我们不需要group_by中的Flow$

library(dplyr)
Flow %>%
    group_by(year, month) %>% 
    summarise(Nas = sum(is.na(Q)))
# A tibble: 2 x 3
# Groups:   year [1]
#   year month   Nas
#  <dbl> <dbl> <int>
#1  1980     1    28
#2  1980     2    19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R-如何将一个空的POSIXct列添加到已经存在的data.frame / tibble中? - R - How can I add an empty POSIXct column to a data.frame / tibble which already exists? 如何将 xml 节点和关键值提取到 R studio 中的 data.frame,包括 NA 值? - How can I extract xml nodes and key values to data.frame in R studio, including NA values? 如何求和 data.frame 列值? - How to sum data.frame column values? R:按特定列获取data.frame组中列的总和 - R : Getting the sum of columns in a data.frame group by a certain column 如何在R中使用值列和标签列可视化data.frame? - How can I visualize a data.frame with a values column and a label column in R? 对 R 中 data.frame 的每一列中的所有值求和 - Sum all values in every column of a data.frame in R 在R中,有没有办法在data.frame的整数列中处理NA,以便在子集化时不包括NA值? - In R, is there a way to handle NA in an integer column of a data.frame so that NA values are not included when subsetting? R - 将 Data.frame 元素转换为单列 tibble() - R - Transform Data.frame elements into a single column tibble() Append 总计行和/或列到 R 中的 data.frame - Append a totals row and/or column to a data.frame in R 如何在R中将列表列表转换为整洁的小标题或data.frame - How to convert list of list into tidy tibble or data.frame in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM