[英]Create a list from a column based on strings in another
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:我想使用关键字及其相关关键字创建列表,我已经尝试使用 map 并保留函数,但我得到一个空列表,我不知道如何解决它:
a <- c(unique(data$main_word))
words <- map(a,keep(data$related,data$main_word == a))
but I get this:但我明白了:
[[1]]
NULL
[[2]]
NULL
We can use split , then add ( c -concatenate) names with mapply , see:我们可以使用split ,然后使用 mapply 添加( c -concatenate )名称,请参阅:
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 <- 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.