2

I am trying to add quotes to a list which is output from predict in recommenderlab library. which looks something as below. I am facing challenge to get this into a concatenated string something like

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

expected:

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

tried different ways

used:

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

Edited for more information

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")

when you see the list here it will be in above mentioned format :

$`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)"                          

I want it to be like :

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

all concatenated as one string, just like above

Thanks but

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

above also not what I am looking as this gives me "\", please let me know what can be done

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

-- This worked . Thanks All

However, I have a small challenge here Want to keep the resultant of the above in dataframe where first column will be userid and second column will be concatenated list. please help!

Santhosh
  • 19
  • 5
  • 6
    Welcome to SO! Please, add some data to use, to copy and paste in R, maybe updating the question with the result of `dput(your_data_here)` if they can be posted. Also, try to share any kind of attempt you've done, to have more chance to have an answer, and not getting downvotes or your question close. – s__ Dec 19 '18 at 12:48

1 Answers1

0

I think you are looking for shQuote()

list1 = as(pre, "list")
paste0(shQuote(list1$`291`),collapse=",")
Fino
  • 1,774
  • 11
  • 21
  • This gives me something like below: "\"Titanic (1997)\",\"Contact (1997)\",\"Alien (1979)\",\"Amadeus (1984)\",\"Godfather, The (1972)\",\"Aliens (1986)\",\"Sting, The (1973)\",\"American Werewolf in London, An (1981)\",\"Schindler's List (1993)\",\"Glory (1989)\"" -- I dont want those "\" – Santhosh Dec 19 '18 at 14:24
  • `cat(paste0(shQuote(list1[["291"]]),collapse=","))` – s_baldur Dec 19 '18 at 14:42
  • Awesome!! Thanks a ton.. :) – Santhosh Dec 19 '18 at 14:47
  • Can you please share it as answer? I will mark as accepted or answered... – Santhosh Dec 19 '18 at 16:24