首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从m/d/y到y/m的出现次数总和

从m/d/y到y/m的出现次数总和
EN

Stack Overflow用户
提问于 2019-03-29 04:29:18
回答 2查看 33关注 0票数 0

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

代码语言:javascript
复制
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)

输出应如下所示:

代码语言:javascript
复制
Date       AvalancheTotal
2000-01           1
2000-02           2
2000-03           8
2000-12           23
2001-01           16
.
.
.
.
.
2019-03            45
EN

回答 2

Stack Overflow用户

发布于 2019-03-29 04:33:10

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

代码语言:javascript
复制
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())

如果我们只需要从December输出到March,那么filter数据

代码语言:javascript
复制
subOut <- out %>%
            filter(as.integer(substr(Date, 6, 7)) %in% c(12, 1:3))

或者它可以在链的更早的时候被filter

代码语言:javascript
复制
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填充

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2019-03-29 04:56:29

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

代码语言:javascript
复制
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())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55406364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档