'Error Iterating Loop over Multiple States with USGS DataRetrevial

I need to run the whatNWISdata() function in the dataRetrieval package (https://waterdata.usgs.gov/blog/dataretrieval/) with the parameter code 62614 for every state and Washington D.C., except for Alaska and Hawaii.

library(dataRetrieval)
stateCdsubset <- subset(stateCd, STATE=="01"|STATE>="04"&STATE<="13"|STATE>="16"&STATE<="56")
for(st in stateCdsubset$STUSAB){
  USASites <- whatNWISdata(stateCd = st, parameterCd = "62614")}

But I keep getting the error "no loop for break/next, jumping to top level." When I try to run it with every state, as below:

library(dataRetrieval)
for(st in stateCd$STATE){
  USASites <- whatNWISdata(stateCd = st, parameterCd = "62614")}

I get the error "Error in if (values["stateCd"] == "UM") { : missing value where TRUE/FALSE needed." I'm not sure what I'm doing wrong.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source