简体   繁体   English

填写缺少日期并添加“0”

[英]Filling in missing dates and adding “0's”

The code below produces the number of avalanches in SLC by each year-month during the ski season (Dec-Mar). 下面的代码在滑雪季节(12月至3月)每年产生SLC的雪崩数量。 Since this code gets the total each year-month, it does not add in the the year-months that had 0 avalanches. 由于此代码获得每年 - 每月的总数,因此它不会增加有0次雪崩的年 - 月。 How do I fill in my table so it will provide all year-month? 如何填写我的表格,以便提供所有年份?

# write the webscraper
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)


# convert the dates and get the  total the number of avalanches
avalancheslc <- avalancheslc %>% 
          group_by(Date = format(as.yearmon(Date, "%m/%d/%Y"), "%Y-%m")) %>% 
          summarise(AvalancheTotal = n())
# pipe to only include Dec-Mar of each year
avalancheslc <- avalancheslc %>% filter(as.integer(substr(Date, 6, 7)) %in% c(12, 1:3))
# the data right now looks like this
Date   AvalancheTotal
1980-01        1
1981-02        1
.
.
.



# the data needs to look like this
Date   AvalancheTotal
1980-01        1
1980-02        0
1980-03        0
1980-12        0
1981-01        0
1981-02        1
1981-03        1
library("tidyverse")
library("lubridate")

# You data here...

# Simpler version
avalancheslc %>%
  separate(Date, c("year", "month")) %>%
  # Some years might be missing (no avalanches at all)
  # We can fill in those with `full_seq` but
  # `full_seq` works with numbers not characters
  mutate(year = as.integer(year)) %>%
  complete(year = full_seq(year, 1), month,
           fill = list(AvalancheTotal = 0)) %>%
  unite("Date", year, month, sep = "-")

# Alternative version (fills in all months, so needs filtering afterwards)

avalancheslc <- avalancheslc %>%
  # In case `Date` needs parsing
  mutate(Date = parse_date_time(Date, "%y-%m"))

# A full data frame of months
all_months <- avalancheslc %>%
  expand(Date = seq(first(Date), last(Date), by = "month"))

# Join to `avalanches` and fill in with 0s
avalancheslc %>%
  right_join(all_months) %>%
  replace_na(list(AvalancheTotal = 0))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM