簡體   English   中英

在 function 中使用時包含多個單詞變量

[英]Including more than one word variables when using it in a function

我正在嘗試制作一個基本的 function ,它采用郵政編碼並輸出 zip 代碼中案例的條形圖。 它適用於一字縣(例如傑斐遜縣),但不適用於二字縣(例如聖克萊爾縣)。 我該如何解決?

library(httr) 
url <- "https://raw.githubusercontent.com/scpike/us-state-county-zip/master/geo-data.csv"
zipcodeData <- content(GET(url), type = 'text/csv')

zipCaseGraph<- function(zip) {
  tbl.county <- subset(zipcodeData, zipcode == zip) 
  countyZip<- tbl.county$county
  stateZip<- tbl.county$state
  url <- paste0("https://knowi.com/api/data/ipE4xJhLBkn8H8jisFisAdHKvepFR5I4bGzRySZ2aaXlJgie?entityName=County%207%20day%20growth%20rates&exportFormat=csv&c9SqlFilter=select%20*%20where%20County%20like%20", countyZip, "%20County%20AND%20State%20like%20", stateZip)
  tbl.countyStats <- content(GET(url), type = 'text/csv')
  tbl.countyCases <- subset(tbl.countyStats, Type!="Deaths") #only has cases not death stats
  tbl.countyCases <- tbl.countyCases[seq(dim(tbl.countyCases)[1],1),] #flip row order so that stats start from earliest date and ends with latest date
  barplot(tbl.countyCases$"7_day_count", main=sprintf("Weekly COVID-19 Case Count in %s County", countyZip), 
          xlab="Weeks since 1/29/20", ylab="Weekly Infection Count")
}

將 URL 中的空格替換為%20

suppressPackageStartupMessages(library(httr))
url <- "https://raw.githubusercontent.com/scpike/us-state-county-zip/master/geo-data.csv"
zipcodeData <- content(GET(url), type = 'text/csv')

zipCaseGraph<- function(zip) {
    tbl.county <- subset(zipcodeData, zipcode == zip) 
    countyZip <- tbl.county$county
    stateZip <- tbl.county$state
    stateAbbr <- tbl.county$state_abbr
    url <- paste0("https://knowi.com/api/data/ipE4xJhLBkn8H8jisFisAdHKvep",
                  "FR5I4bGzRySZ2aaXlJgie?entityName=County%207%20day%20growth",
                  "%20rates&exportFormat=csv&c9SqlFilter=select%20*%20where",
                  "%20County%20like%20", 
                  gsub(" " , "%20", countyZip), 
                  "%20County%20AND%20State%20like%20", 
                  gsub(" " , "%20", stateZip))
    tbl.countyStats <- content(GET(url), type = 'text/csv')
    tbl.countyCases <- subset(tbl.countyStats, Type!="Deaths") #only has cases not death stats
    tbl.countyCases <- tbl.countyCases[seq(dim(tbl.countyCases)[1],1),] #flip row order so that stats start from earliest date and ends with latest date
    barplot(tbl.countyCases$"7_day_count", 
        main=sprintf("Weekly COVID-19 Case Count in %s, %s", countyZip, stateAbbr), 
        xlab="Weeks since 1/29/20", ylab="Weekly Infection Count")
}

zipCaseGraph("08243")

reprex package (v0.3.0) 於 2020 年 8 月 12 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM