Reputation: 4290
I use igraph
package in R
for Social Network Analysis. I decide to work with Movielens Dataset (Movies Section)
, I also loaded the igraph Library
, when I wanted to work with adjacency matrix.
The dataset loaded successfully, and these r my codes.
ff = read.csv("D:/TMU/DataSet/MovieLens/movies.csv", header = TRUE)
ff
mtr = as.matrix(ff)
gr = graph.adjacency(mtr, mode = "undirected", weighted = NULL, diag = FALSE)
I faced with this error :
Error in graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted, :
At structure_generators.c:274 : Non-square matrix, Non-square matrix
In addition: Warning message:
In mde(x) : NAs introduced by coercion
is there a problem with dataset or what ?
Upvotes: 1
Views: 2322
Reputation: 25894
Okay, using the small dataset from https://grouplens.org/datasets/movielens/ which has dimension 9125x3
Download the data (you may need to tweak the mode
in the download.file
if you are using windows)
pth <- "http://files.grouplens.org/datasets/movielens/ml-latest-small.zip"
download.file(pth, destfile=temp<-tempfile())
#unzip(temp, list=TRUE) # see what files?
unzip(temp, exdir=td<-tempdir())
# read movies dataset
movies <- read.csv(file.path(td, "ml-latest-small/movies.csv"),
header=TRUE, stringsAsFactors = FALSE)
Load some libraries
library(tm) # to form the binary matrix: best to keep things sparse
library(slam) # for the crossproduct of the simple_triplet_matrix returned by tm::DocumentTermMatrix
library(igraph)
Form binary matrix for movies by genres (had to use MrFlick's suggestion of VCorpus otherwise "(no genres listed)" and "film-noir" were split into the individual words
# split the genres string and create binary matrix for presence of genre
corp <- VCorpus(VectorSource(movies$genres))
dtm <- DocumentTermMatrix(corp,
control = list(tokenize = function(x)
unlist(strsplit(as.character(x), "\\|"))))
Create adjacency matrix
# this looks for agreement across the genres
# you could use tcrossprod for similarities on the films
adj <- crossprod_simple_triplet_matrix(dtm)
Create graph
g <- graph_from_adjacency_matrix(adj, mode="undirected", weighted=TRUE)
Upvotes: 0