0

I have a with multiple tbl's nested inside a list. All of the tbl df's have a column for ZIPCode. I have a table where I received data from the zipcode package so I could obtain latitude and longitude for each of the zipcodes.

I'd like to left join the latitude and longitude onto each of the tbl's so I could use later for leaflet. I tried to set it up such as:

zip = lapply(myfiles, function(x){
z = left_join(myfiles, zip_filtered, by=c("ZIPCode"="zip"))
return(z)
})

Which throws the following error:

Error in UseMethod("left_join") : no applicable method for 'left_join' applied to an object of class "list"

I then tried the following to pass this through:

zip = lapply(myfiles, function(x){
z = x[,left_join(x, zip_filtered, by=c("ZIPCode"="zip"))]
return(z)
})

Which gives me the following: Error: Unsupported index type: tbl_df.

The best scenario would be just to append the two columns latitude and longitude to all six tbl_df's in the list but unsure how to do that which is why I started going down this route.

Thanks!

Drthm1456
  • 409
  • 9
  • 17
  • 3
    Possible duplicate of [Simultaneously merge multiple data.frames in a list](https://stackoverflow.com/questions/8091303/simultaneously-merge-multiple-data-frames-in-a-list) – Cristian E. Nuno Oct 04 '18 at 17:02
  • 1
    Your first `lapply` uses anonymous `function(x)`, but it never uses `x`. In side the anonymous function, replace `myfiles` with `x`. – Gregor Thomas Oct 04 '18 at 17:04
  • Given your remark about just appending columns, if you don't need to `join` (because you know the rows are in the right order already) then `zip = lapply(myfiles, cbind, zip_fiiltered)` – Gregor Thomas Oct 04 '18 at 17:06
  • you can also `rbindlist()` the different tbl't together (you van use the argument `idcol` to keep the different table in differenst groups), and then left_join `y[x, on...]` the lat/lon coordinates – Wimpel Oct 05 '18 at 09:57
  • [another similar question got an answer here](https://stackoverflow.com/questions/56156871/join-tibbles-in-list-to-one-tibble) – Andrés Parada Mar 02 '22 at 15:28

1 Answers1

0

Try this as seen here

myfiles %>% reduce(left_join)
Andrés Parada
  • 319
  • 7
  • 21