rrecommenderlab

R list save as quoted list


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


Solution

  • 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:

    movies <- cat(paste0(shQuote(list1[["291"]]),collapse=","))
    theData <- data.frame(name="Santhosh",movies,stringsAsFactors=FALSE)
    

    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 and dplyr) can be used to produce this data frame.

    library(tidyr)
    library(dplyr)
    recommendedMovies <- c("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)")
    theData <- data.frame(name="Santhosh",
                          rank=1:length(recommendedMovies),
                          movies=recommendedMovies,stringsAsFactors=FALSE)
    theData %>% group_by(name) %>%
    spread(.,rank,movies,sep="movie")
    

    ...and the output:

    > theData %>% group_by(name) %>%
    + spread(.,rank,movies,sep="movie")
    # A tibble: 1 x 11
    # Groups:   name [1]
      name  rankmovie1 rankmovie2 rankmovie3 rankmovie4 rankmovie5 rankmovie6 rankmovie7 rankmovie8 rankmovie9
      <chr> <chr>      <chr>      <chr>      <chr>      <chr>      <chr>      <chr>      <chr>      <chr>     
    1 Sant… Titanic (… Contact (… Alien (19… Amadeus (… Godfather… Aliens (1… Sting, Th… American … Schindler…
    # ... with 1 more variable: rankmovie10 <chr>
    >