rzipcoder

assign values inside a function based on specific columns in a data frame


I'm looking for direction on assigning a data frame column value to a specific place in a function and then looping or something to create a series of objects to be bound into a longer table.

example data

a = c("17","17","29")
b = c("133","163","055")
data.frame(a, b)

doing this manually...

library(zipcodeR)

T1 <- search_fips("17", "133")
T2 <- search_fips("17", "163") 
T3 <- search_fips("29", "055")



TT <- list(T1, T2, T3)
CZ_zips <- rbindlist(TT, use.names=TRUE, fill=TRUE)

would like a to read a and b columns into a set place in function to create a series of vectors or data frames that can then be bound into one longer table.

the search_fips function pulls out of the census FIPS data, a = state and b = county. package is zipcodeR.


Solution

  • One simple way is to wrap the search_fips() function in a lapply function and rest stays the same.

    library(zipcodeR)
    
    a = c("17","17","29")
    b = c("133","163","055")
    df<-data.frame(a, b)
    
    output <-lapply(1:nrow(df), function(i) {
       search_fips(df$a[i], df$b[i])
    })
      
    answer <- dplyr::bind_rows(output)