1

I have the following tibble with two columns, the first has keywords and the second has any related words to the keyword:

    # A tibble: 14 x 2
   main_word related        
   <chr>     <chr>          
 1 car       rent car       
 2 car       buy car        
 3 car       mechanic       
 4 car       car rent       
 5 plane     plane ride     
 6 plane     plane ticket   
 7 plane     cheap flights  
 8 plane     book flight    
 9 plane     easyjet        
10 plane     lufthansa      
11 plane     british airways
12 plane     ryan air       
13 plane     air france     
14 plane     fly emirates  

I want to create lists with the keywords and their related keywords, I've tried this using the map and keep functions but I get an empty list and I can't figure out how to solve it:

a <- c(unique(data$main_word))
  words <- map(a,keep(data$related,data$main_word == a))

but I get this:

[[1]]
NULL

[[2]]
NULL
NelsonGon
  • 13,015
  • 7
  • 27
  • 57

1 Answers1

1

We can use split, then add (c-concatenate) names with mapply, see:

x <- split(data$related, data$main_word)

myList <- mapply(c, names(x), x)
myList

$car
[1] "car"      "rent car" "buy car"  "mechanic" "car rent"

$plane
 [1] "plane"           "plane ride"      "plane ticket"    "cheap flights"  
 [5] "book flight"     "easyjet"         "lufthansa"       "british airways"
 [9] "ryan air"        "air france"      "fly emirates" 

data

data <- read.table(text = "
main_word,related
car,rent car
car,buy car
car,mechanic
car,car rent
plane,plane ride
plane,plane ticket
plane,cheap flights
plane,book flight
plane,easyjet
plane,lufthansa
plane,british airways
plane,ryan air
plane,air france
plane,fly emirates", header = TRUE, sep = ",", stringsAsFactors = FALSE)
zx8754
  • 52,746
  • 12
  • 114
  • 209