3

I have two datasets. The first one contains a list of cities and their distance in miles from destinations. The second list contains the destinations. I want to use purrr to put the name of the closest destination into a new column in the first dataset.

Here's the first dataset (with made up data/distances):

library(tidyverse)
data1 <- tibble(city = c("Atlanta", "Tokyo", "Paris"),
                   dist_Rome = c(1000, 2000, 300),
                   dist_Miami = c(400, 3000, 1500),
                   dist_Singapore = c(3000, 600, 2000),
                   dist_Toronto = c(900, 3200, 1900))

Here's the second dataset with the destinations:

library(tidyverse)
data2 <- tibble(destination = c("Rome Italy", "Miami United States", "Singapore Singapore", "Toronto Canada"))

And here's what I want it to look like:

library(tidyverse)
solution <- tibble(city = c("Atlanta", "Tokyo", "Paris"),
                   dist_Rome = c(1000, 2000, 300),
                   dist_Miami = c(400, 3000, 1500),
                   dist_Singapore = c(3000, 600, 2000),
                   dist_Toronto = c(900, 3200, 1900),
                   nearest = c("Miami United States", "Singapore Singapore", "Rome Italy"))

I'm ideally looking for a tidy solution, and I have attempted to do this with purrr but to no avail. This is my failed attempt:

library(tidyverse)
solution <- data1 %>%
  mutate(nearest_hub = map(select(., contains("dist")), ~
                                  case_when(which.min(c(...)) ~ data2$destination),
                                TRUE ~ "NA"))
Error in which.min(c(...)) : 
  (list) object cannot be coerced to type 'double'

Thank you!

J.Sabree
  • 2,280
  • 19
  • 48

2 Answers2

2

We can gather into 'long' format, grouped by 'city', slice the rows with the minimum 'val', left_join with 'data2' to get the 'nearest'

library(tidyverse)
data1 %>% 
   gather(key, val, starts_with("dist")) %>% 
   group_by(city) %>% 
   slice(which.min(val)) %>% 
   ungroup %>%
   transmute(city, key = str_remove(key, 'dist_')) %>% 
   left_join(data2 %>% 
   mutate(key = word(destination, 1))) %>%
   select(city, nearest = destination) %>% 
   left_join(data1)
akrun
  • 874,273
  • 37
  • 540
  • 662
2

A solution using the tidyverse.

library(tidyverse)

data3 <- data1 %>%
  mutate(City = apply(data1 %>% select(-city), 1, function(x) names(x)[which.min(x)])) %>%
  mutate(City = str_remove(City, "^dist_")) %>%
  left_join(data2 %>%
              separate(destination, into = c("City", "Country"), sep = " ", remove = FALSE),
            by = "City") %>%
  select(-City, -Country) %>%
  rename(nearest = destination)

data3
# # A tibble: 3 x 6
#   city    dist_Rome dist_Miami dist_Singapore dist_Toronto nearest            
#   <chr>       <dbl>      <dbl>          <dbl>        <dbl> <chr>              
# 1 Atlanta      1000        400           3000          900 Miami United States
# 2 Tokyo        2000       3000            600         3200 Singapore Singapore
# 3 Paris         300       1500           2000         1900 Rome Italy
www
  • 38,575
  • 12
  • 48
  • 84