简体   繁体   English

根据另一列中的字符串从列创建列表

[英]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数据

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.

相关问题 根据 R 中的另一列创建从列中提取的列分组字符串文本 - Create a column grouping strings text extracted from a column based on another column in R 根据字符串列表查找出现在一列中但在另一列中缺失的字符串 - finding strings that appear in one column but missing from another based on a list of strings 根据另一列的字符串内容创建新列 - Create new columns based on the content of strings of another column 根据R中相同列表中的字符串创建新列 - Create new column based on strings within the same list in R 如何从另一个文件中的列创建列表? - How to create a list from a column in another file? R:根据另一列中的重复字符串连接一列中的字符串 - R: Concatenate strings from one column based on repeating strings in another column 根据年份的另一列中的特定条件创建变量 - Create a variable based on specific condition from another column based on Year 根据另一列R中的字符创建一列 - Create a column based on characters from another column R 使用 dplyr 根据来自另一列的值的总和创建一个新列 - Create a new column based on the sum of values from another column, with dplyr 基于来自 dataframe 的另一列,使用 rnorm function 创建新列 - Create new column based on another column from dataframe with rnorm function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM