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

464 views Asked by At

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.

4

There are 4 answers

1
Dave2e On BEST ANSWER

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)
1
Rui Barradas On

Here is a solution with Map.
The two one-liners below are equivalent, the first is probably more readable but the other one is simpler.

library(zipcodeR)

a <- c("17", "17", "29")
b <- c("133", "163", "055")
df <- data.frame(a, b)

Map(function(x, y) search_fips(x, y), df$a, df$b)

result <- Map(search_fips, df$a, df$b)
result <- dplyr::bind_rows(result)
head(result)
#> # A tibble: 6 x 24
#>   zipcode zipcode_type major_city post_office_city common_city_list county state
#>   <chr>   <chr>        <chr>      <chr>                      <blob> <chr>  <chr>
#> 1 62236   Standard     Columbia   Columbia, IL           <raw 20 B> Monro~ IL   
#> 2 62244   Standard     Fults      Fults, IL              <raw 17 B> Monro~ IL   
#> 3 62248   PO Box       Hecker     Hecker, IL             <raw 18 B> Monro~ IL   
#> 4 62256   PO Box       Maeystown  <NA>                   <raw 21 B> Monro~ IL   
#> 5 62279   PO Box       Renault    Renault, IL            <raw 19 B> Monro~ IL   
#> 6 62295   Standard     Valmeyer   Valmeyer, IL           <raw 20 B> Monro~ IL   
#> # ... with 17 more variables: lat <dbl>, lng <dbl>, timezone <chr>,
#> #   radius_in_miles <dbl>, area_code_list <blob>, population <int>,
#> #   population_density <dbl>, land_area_in_sqmi <dbl>,
#> #   water_area_in_sqmi <dbl>, housing_units <int>,
#> #   occupied_housing_units <int>, median_home_value <int>,
#> #   median_household_income <int>, bounds_west <dbl>, bounds_east <dbl>,
#> #   bounds_north <dbl>, bounds_south <dbl>

Created on 2022-02-18 by the reprex package (v2.0.1)

0
akrun On

Using rowwise

library(dplyr)
library(tidyr)
library(zipcodeR)
out <- df %>% 
 rowwise %>% 
 mutate(result = list(search_fips(a, b))) %>%
 ungroup %>% 
 unnest(result)

-output

> head(out, 2)
# A tibble: 2 × 26
  a     b     zipcode zipcode_type major_city post_office_city common_city_list county        state   lat   lng timezone radius_in_miles area_code_list
  <chr> <chr> <chr>   <chr>        <chr>      <chr>                      <blob> <chr>         <chr> <dbl> <dbl> <chr>              <dbl>         <blob>
1 17    133   62236   Standard     Columbia   Columbia, IL           <raw 20 B> Monroe County IL     38.4 -90.2 Central                7     <raw 15 B>
2 17    133   62244   Standard     Fults      Fults, IL              <raw 17 B> Monroe County IL     38.2 -90.2 Central                7     <raw 15 B>
# … with 12 more variables: population <int>, population_density <dbl>, land_area_in_sqmi <dbl>, water_area_in_sqmi <dbl>, housing_units <int>,
#   occupied_housing_units <int>, median_home_value <int>, median_household_income <int>, bounds_west <dbl>, bounds_east <dbl>,
#   bounds_north <dbl>, bounds_south <dbl>

data

df <- structure(list(a = c("17", "17", "29"), b = c("133", "163", "055"
)), class = "data.frame", row.names = c(NA, -3L))
1
Ruam Pimentel On

here is a loop you might want to put in your function:

library(dplyr)
library(zipcodeR)

my_list <- list()

for (i in 1:nrow(df)) {
  my_list[i] <- search_fips(df$a[i], df$b[i])
  
}

new_df <- bind_rows(my_list)

bind_rows(my_list)