想要将Recommendationerlab预测列表另存为“”分隔列表。我有一个相同的问题,但在这里想稍加扩展。
我已经尝试了几种方法,并在下面找到了相对的方法,但是坚持了将输出放在“”逗号分隔脚本中的简单步骤。
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=","))
上面给了我给定的用户:
"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)"
我想将用户和电影放置在数据框中,其中第一列将是用户,第二列将是上述串联形式的电影
答案 0 :(得分:0)
鉴于cat(paste0(shQuote(list1[["291"]]),collapse=","))
产生了电影推荐字符串,可以执行以下操作将其转换为带有名称标签的数据框:
movies <- cat(paste0(shQuote(list1[["291"]]),collapse=","))
theData <- data.frame(name="Santhosh",movies,stringsAsFactors=FALSE)
另一种方法是将每个影片保存为输出数据帧中的单独列,这将使在R中使用数据更加容易,而不必多次分析影片列表。 tidyverse(即tidyr
和dplyr
)可用于生成此数据帧。
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")
...以及输出:
> 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>
>