Want to save recommenderlab predict list as list of "" seperated list. I have one question in place for the same but here want to extend it with a twist.
I already tried few approaches and found below as relavent but stuck with a simple step of putting the ouptput in "" comma seperated script.
library("recommenderlab")
library(stringi)
data("MovieLense")
MovieLense100 <- MovieLense[rowCounts(MovieLense) >100,]
MovieLense100
train <- MovieLense100[1:50]
rec <- Recommender(train, method = "UBCF")
rec
pre <- predict(rec, MovieLense100[101:105], n = 10)
as(pre, "list")
list1 = as(pre, "list")
cat(paste0(shQuote(list1[["291"]]),collapse=","))
The above gives me for given user:
"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 want to put user and movies in dataframe where first column will be user and second column will be movies in above concatenated form
Given that
cat(paste0(shQuote(list1[["291"]]),collapse=","))
produces the string of movie recommendations, one could do the following to turn this into a data frame tagged with a name:Another approach would be to save each movie as a separate column in the output data frame, which would make it easier to use the data in R without having to parse the movie list multiple times. The tidyverse (i.e.
tidyr
anddplyr
) can be used to produce this data frame....and the output: