【问题标题】:Summing the number of occurrences from m/d/y to y/m将 m/d/y 到 y/m 的出现次数相加
【发布时间】:2019-03-29 05:39:19
【问题描述】:

我有每次发生雪崩的数据。我需要计算每年和每月发生的雪崩数量,但数据只是给出了雪崩发生的确切日期。我如何总结每年每月发生的事件数?我也只需要与冬季相关的年份月份(Dec (12) - March (3))。请帮忙!

library(XML)
library(RCurl)
library(dplyr)
avalanche<-data.frame()
avalanche.url<-"https://utahavalanchecenter.org/observations?page="
all.pages<-0:202
for(page in all.pages){
  this.url<-paste(avalanche.url, page, sep="")
  this.webpage<-htmlParse(getURL(this.url))
  thispage.avalanche<-readHTMLTable(this.webpage, which=1, header=T,stringsAsFactors=F)
  names(thispage.avalanche)<-c('Date','Region','Location','Observer')
  avalanche<-rbind(avalanche,thispage.avalanche)
}

# subset the data to the Salt Lake Region
avalancheslc<-subset(avalanche, Region=="Salt Lake")
str(avalancheslc)

输出应该类似于:

Date       AvalancheTotal
2000-01           1
2000-02           2
2000-03           8
2000-12           23
2001-01           16
.
.
.
.
.
2019-03            45

【问题讨论】:

    标签: r


    【解决方案1】:

    使用 dplyr,您可以从 Date 列中获取感兴趣的变量(“年-月”),按此变量分组,然后计算每个组中的行数。 以类似的方式,您可以过滤以仅获取您喜欢的月份:

    library(dplyr)
    winter_months <- c(1:3, 12)
    
    avalancheslc %>% 
        mutate(Date = as.Date(Date, "%m/%d/%Y")) %>% 
        mutate(YearMonth = format(Date,"%Y-%m"), 
               Month = as.numeric(format(Date,"%m"))) %>%
        filter(Month %in% winter_months) %>%
        group_by(YearMonth) %>%
        summarise(AvalancheTotal = n())
    
    

    【讨论】:

    • 如何填写所有这些记录之间缺少的日期?有些年月没有雪崩。例如,1980-01 发生了 1 次雪崩,但下一个数据点要到 1981-02。如何在 AvalancheTotal 下填写 1980-02、1980-03、1980-12 等的日期和“O”?
    • 创建另一个数据框,在输出中列出您想要的 所有 个月,将输出数据框左连接到它,并将 NA 值替换为 0?
    【解决方案2】:

    我们可以从 zoo 转换为 yearmon 并在 group_by 中使用它来获取行数

    library(dplyr)
    library(zoo)
    
    dim(avalancheslc)
    #[1] 5494    4
    out <- avalancheslc %>% 
              group_by(Date = format(as.yearmon(Date, "%m/%d/%Y"), "%Y-%m")) %>% 
              summarise(AvalancheTotal = n())
    

    如果我们只需要从DecemberMarch 的输出,那么filter 的数据

    subOut <- out %>%
                filter(as.integer(substr(Date, 6, 7)) %in% c(12, 1:3))
    

    或者它可以是链中更早的filtered

    library(lubridate)
    out <- avalancheslc %>%
             mutate(Date = as.yearmon(Date, "%m/%d/%Y")) %>%
             filter(month(Date) %in% c(12, 1:3))  %>% 
             count(Date)
    dim(out)
    #[1] 67  2
    

    现在,用 0 填充

    mths <- month.abb[c(12, 1:3)]
    out1 <- crossing(Months = mths, 
                Year = year(min(out$Date)):year(max(out$Date))) %>%
           unite(Date, Months, Year, sep= " ") %>% 
           mutate(Date = as.yearmon(Date)) %>% 
           left_join(out) %>% 
           mutate(n = replace_na(n, 0)) 
    
    tail(out1)
    # A tibble: 6 x 2
    #  Date              n
    #  <S3: yearmon> <dbl>
    #1 Mar 2014        100
    #2 Mar 2015         94
    #3 Mar 2016         96
    #4 Mar 2017         93
    #5 Mar 2018        126
    #6 Mar 2019        163
    

    【讨论】:

    • 这行得通,但我怎么能让它只包括每年的 12 月至 3 月?此外,如果一年中没有 Dec、Jan、Feb 或 Mar 的值......我怎么能得到它 =0?
    • @crich 如果你查看subOut,它是从 'Dec-Mar 过滤的
    • subOut 有效!现在,我需要在 12 月至 3 月范围内添加没有雪崩的日期...如何添加这些缺少 0 雪崩的日期
    • 如何填写 subOut 的所有这些记录之间缺少的日期?有些年月没有雪崩。例如,1980-01 发生了 1 次雪崩,但下一个数据点要到 1981-02。我如何在 AvalancheTotal 下填写 1980-02、1980-03、1980-12 等的日期和“O”?我想保留年月的格式,以便可以将其与另一个具有年月变量的数据集合并
    • @crich 请检查“out1”而不是“subOut”。满了
    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2022-01-20
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多