1

I have six dataframe (all_road_25, all_road_50, all_road_100,all_road_300, all_road_500, all_road_1000) and all of the data frame contain the same column "site" and another column "length". I want to join all of them by the same column "site" and with the rest columns names showing the original dataframe. So I tried:

all_roads_variables<- list(all_road_25, all_road_50, all_road_100,
                       all_road_300, all_road_500, all_road_1000) %>% 
                    reduce(full_join, by = "site") 
names(all_roads_variables)[2:7] <- c("all_road_25","all_road_50","all_road_100",
                                 "all_road_300", "all_road_500", "all_road_1000 ")

It gives the results I want, but I have to copy all the names of original dataframes by hand. Is there a way of making the scripts shorter?

  • How did you wind up with six data.frames in the first place? Did you have to type all those by hand at one point? It seems like these should already be in a list to begin with. There are lots of ways to make that part easier. – MrFlick Aug 02 '18 at 15:17
  • Related: https://stackoverflow.com/questions/16951080/can-lists-be-created-that-name-themselves-based-on-input-object-names – MrFlick Aug 02 '18 at 15:17
  • @MrFlick I did buffer for all the roads with different radii and typed all the values by hand. I should have use lapply for this. Thanks for the suggestion! – user8953332 Aug 03 '18 at 10:36

1 Answers1

1

We can use mget to do this. It will give a named list of all the datasets that have similar pattern names

all_roads_variables <- mget(ls(pattern = "^all_road_\\d+"))
akrun
  • 874,273
  • 37
  • 540
  • 662