【问题标题】:Counting number of Rainfall as Raindays将降雨量计算为雨天
【发布时间】:2020-03-10 16:02:19
【问题描述】:

我想计算一个月内不同地点不同年份下雨的天数。

这是我的数据:

Location Year Month Day Precipitation
A        2008 1     1   0
A        2008 1     2   8.32
A        2008 1     3   4.89
A        2008 1     4   0

我最多有 18 个地点,年份是从 2008 年到 2018 年,每年 12 个月,降水量为 0 表示当天没有下雨。

【问题讨论】:

标签: r dataframe count aggregate


【解决方案1】:

你可以使用aggregate:

aggregate(cbind(days=x$Precipitation > 0), as.list(x[c("Location", "Year", "Month")]), sum)
#  Location Year Month days
#1        A 2008     1    2

数据:

x <- structure(list(Location = structure(c(1L, 1L, 1L, 1L), .Label = "A", class = "factor"), 
    Year = c(2008L, 2008L, 2008L, 2008L), Month = c(1L, 1L, 1L, 
    1L), Day = 1:4, Precipitation = c(0, 8.32, 4.89, 0)), class = "data.frame", row.names = c(NA, -4L))

【讨论】:

    【解决方案2】:

    根据现有信息

    df <- df %>%
      filter(Precipitation != 0) %>%
      group_by(Location, Year, Month) %>%
      summarize(DaysOfRain = n())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2014-09-13
      • 2021-09-15
      相关资源
      最近更新 更多