[R] Problem creation tensor

Peppe Ricci peppepegasus at gmail.com
Tue Jul 17 12:31:38 CEST 2012

Hi guys,

I need some help to analyzing my data.
I start to describe my data: I have 21 matrices, every matrix on the
rows has users and on columns has items, in my case films.
Element of index (i, j) represent the rating expressed by user i about item j.
I have a matrix for each of professions.
An example of a this type of matrix is:

                    item 1    item 2    item 3    item4
  id user 1        1          ?              ?           5
  id user 2        ?          3              3           ?
  id user 3        2          ?              3           2
  id user 4        ?          ?              ?           4
So user 1 don't like item 1 but he likes so much item 4, for item 2
and 3 he hasn't expressed a rating, etc.
I need to construct a tensor with n users, m items and 21 occupations.
After I have construct the tensor I want apply Parafac.
I read data from a CSV file and build each matrix for each occupation.

Didier Leibovici (author of PTAk package) suggested to me:

ok that's bit clearer you have 21 matrices ( 1 for each occupations)
of users rating their preferences (from 1 to 5 but without rating all
of them: missing values) of  m items.
but I suppose the users are not the same across the 21 occupations
(one has only one occupation .... if you're talking about
working/living occupation)
so you can't create a tensor n users x m items x 21 occupations
but you can build the contingencies of preferences m items x 21
occupations x 5 ratings

One way to build your tensor m x 21 x 5 is:
M1 is the first occupation (users x m) ...
UserItem <-rbind(M1,M2, ...M21)


for (j in 1:m){
    UserItem[,j] =factor(UserItem[,j],levels=1:5)

Z <- array(rep(0,m*21*5),c(m,21,5),
for ( i in 1:m){
  as.matrix(table(occ, UserItem[,2]))
  Z[i,,]=table(occ, UserItem[,i])

Z.CAND <- CANPARA(Z,dim=7)

I have implemented this code but I have one error in correspondance of:

  for ( i in 1:m){

and error is:

Error in
the number of elements to be replaced is not a multiple of the length
of substitution

Can anyone help me to understand this code and how I can resolve the error?
Best regards.

More information about the R-help mailing list