Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendation for modifying get_qwi for collecting variables based on different values of firmsize and firmage #29

Open
monaahmadiani opened this issue May 14, 2020 · 0 comments

Comments

@monaahmadiani
Copy link
Collaborator

monaahmadiani commented May 14, 2020

For firmage:

collector <- list()
collect_industry <- dplyr::data_frame()

for (j in seq_along(state)) {
    st <- state[[j]]
    
for (k in seq_along(year)) {
      yr <- year[[k]]
    
for (i in seq_along(industry)) {
  ind <- industry[[i]]
    
    url <-paste("https://api.census.gov/data/timeseries/qwi/sa?get=",
                "Emp,EmpEnd,HirA,Sep,FrmJbC,FrmJbCS,FrmJbGn,FrmJbLs,sEmp,sEmpEnd,sHirA,sSep,sFrmJbC,sFrmJbCS,sFrmJbGn,sFrmJbLs,EarnS,EarnBeg,EarnHirAS,EarnHirNS,EarnSepS,sEarnS,sEarnBeg,sEarnHirAS,sEarnHirNS,sEarnSepS",
                "&for=metropolitan+statistical+area/micropolitan+statistical+area",
                "&in=state:",st,
                "&year=",yr,
                "&quarter=1&quarter=2&quarter=3&quarter=4",
                "&sex=0",
                "&agegrp=A00",
                "&ownercode=A05",
                "&firmage=1&firmage=2&firmage=3&firmage=4&firmage=5",
                "&seasonadj=U&",
                "industry=",ind,
                "&key=",census_key,
                sep="")

    
    call <- httr::GET(url)
    #print(call$status_code)
    if(!call$status_code %in% c(200, 202)){
      # 500 means that message failed If not 500 then there was an OK
      next(i)
      print(call$status_code)
      print(url)
      
    } else{
      # Keep going if there isn't an error
      dat <- dplyr::as_data_frame(
        jsonlite::fromJSON(
          httr::content(call, as = "text")))
      
      colnames(dat) <- dat[1, ]
      dat <- dat[-1, ]
      
      # Keep adding to the data frame
      collect_industry <- dplyr::bind_rows(collect_industry, dat)
    }
    
  }

  # Store for each state into a list
  collector[[i]] <- collect_industry
}
}

And for firmsize:

collector <- list()
collect_industry <- dplyr::data_frame()

for (j in seq_along(state)) {
    st <- state[[j]]
    

for (k in seq_along(year)) {
      yr <- year[[k]]
    
for (i in seq_along(industry)) {
  ind <- industry[[i]]
    
    url <-paste("https://api.census.gov/data/timeseries/qwi/sa?get=",
                "Emp,EmpEnd,HirA,Sep,FrmJbC,FrmJbCS,FrmJbGn,FrmJbLs,sEmp,sEmpEnd,sHirA,sSep,sFrmJbC,sFrmJbCS,sFrmJbGn,sFrmJbLs,EarnS,EarnBeg,EarnHirAS,EarnHirNS,EarnSepS,sEarnS,sEarnBeg,sEarnHirAS,sEarnHirNS,sEarnSepS",
                "&for=metropolitan+statistical+area/micropolitan+statistical+area",
                "&in=state:",st,
                "&year=",yr,
                "&quarter=1&quarter=2&quarter=3&quarter=4",
                "&sex=0",
                "&agegrp=A00",
                "&ownercode=A05",
                "&firmsize=1&firmsize=2&firmsize=3&firmsize=4&firmsize=5",
                "&seasonadj=U&",
                "industry=",ind,
               "&key=",census_key,
                sep="")

    
    call <- httr::GET(url)
    #print(call$status_code)
    if(!call$status_code %in% c(200, 202)){
      # 500 means that message failed If not 500 then there was an OK
      next(i)
      print(call$status_code)
      print(url)
      
    } else{
      # Keep going if there isn't an error
      dat <- dplyr::as_data_frame(
        jsonlite::fromJSON(
          httr::content(call, as = "text")))
      
      colnames(dat) <- dat[1, ]
      dat <- dat[-1, ]
      
      # Keep adding to the data frame
      collect_industry <- dplyr::bind_rows(collect_industry, dat)
    }
    
  }

  # Store for each state into a list
  collector[[i]] <- collect_industry
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant