Merging Data frames into single in array

hi There,
I'm looking for a solution to merge two data frames, into a vector field.

names<-c("Name1", "Name2", "Name3","Name4")
locations<-c("NY,DC,NJ", "DA,MO,CT,AZ", "SL,SV,NJ", "NV,SV,NJ")
players<-data.frame(names, locations)

locs<-c("NY",  "DC",  "NJ",  "DA",  "MO",  "CT", "AZ", "SL", "SV",  "NV")
zips<-c("100", "200", "300", "400", "500", "600","700","800","900", "1000")
zip_map<- data.frame(locs,zips)

im expecting an output as below, i wanted to merge the map the zip_code based on the locations array.

zip<-c("100,200,300", "400,500,600,700", "800,900,300", "1000,900,300")
out_put<-cbind(out_put, v)

please help.

Thank you for your Support.
i made something like this below. closer to your solution

uniqCSV <- function(x) { paste(unique(x), sep = ',') }

pl<-players %>% 
    rowwise %>%
    mutate(zip_code=uniqCSV(filter(zip_map, locs %in% strsplit(locations, ",")[[1]])$zips ))

looking for any other better way ......

output <- players %>% mutate(zips=locations)
purrr::walk2(.x = zip_map$locs,
             .y = zip_map$zips,
             .f = ~{output$zips <<- gsub(pattern = .x, replacement = .y, x = output$zips)})


  names   locations            zips
1 Name1    NY,DC,NJ     100,200,300
2 Name2 DA,MO,CT,AZ 400,500,600,700
3 Name3    SL,SV,NJ     800,900,300
4 Name4    NV,SV,NJ    1000,900,300

Thanks for your help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.