2

我正在尝试将引号添加到从 recommenderlab库中预测输出的列表中。看起来如下所示。我面临着将其变成连接字符串的挑战,例如

List: 
"Key" 
"value1" "value2" "value3" 
"value4" .............. 

预期的:

"value1", "value2", "value3", "value4" .......... 

尝试了不同的方法

用过的:

stri_join_list(v_list, sep = ";", collapse = NULL) 

编辑以获取更多信息

library("recommenderlab")

data("MovieLense")

MovieLense100 <- MovieLense[rowCounts(MovieLense) >100,]

MovieLense100

train <- MovieLense100[1:50]

rec <- Recommender(train, method = "UBCF")

rec

pre <- predict(rec, MovieLense100[101:102], n = 10)

as(pre, "list")

当您在此处看到列表时,它将采用上述格式:

$`291`
 [1] "Titanic (1997)"                         "Contact (1997)"                         "Alien (1979)"                          
 [4] "Amadeus (1984)"                         "Godfather, The (1972)"                  "Aliens (1986)"                         
 [7] "Sting, The (1973)"                      "American Werewolf in London, An (1981)" "Schindler's List (1993)"               
[10] "Glory (1989)"                          

我希望它像:

"Titanic (1997)", "Contact (1997)", "Alien (1979)"  ....

全部连接为一个字符串,就像上面一样

谢谢但是

paste0(shQuote(list1),collapse=",")

上面也不是我想要的,因为这给了我“\”,请让我知道可以做什么

cat(paste0(shQuote(v_list[["bi_Marika77"]]),collapse=";"))

- 这行得通。谢谢大家

但是,我在这里有一个小挑战想要将上述结果保留在数据框中,其中第一列将是用户 ID,第二列将是连接列表。请帮忙!

4

1 回答 1

0

我想你正在寻找shQuote()

list1 = as(pre, "list")
paste0(shQuote(list1$`291`),collapse=",")
于 2018-12-19T13:06:30.867 回答