transformar datos de entrada

i'm struggeling with some transformation in R. My csv file is structured like following:

User Movie Rating
1    34     4
1    55     3
1    24     5
2    55     1
2    67     5
2    24     3

and so on. And I'd like to get a matrix like this (if a user hasn't rated a movie, insert 0 as rating):

24   34    55    67
 5   4     3      0
 3   0     1      5

where each row is a single user and the columns are movies. So each entry is a rating for a movie. I'm wondering if there is a simple solution in R after i've read in the csv above. I try to do a workaround with python at the moment...

Muchas gracias.

saludos

preguntado el 10 de marzo de 12 a las 13:03

-1 R is not part of the gnu project. Have corrected title. Also if the format of your input is as described then its not csv. Please clarify/correct your question. -

1 Respuestas

> inp <- read.table(text="User Movie Rating
+ 1    34     4
+ 1    55     3
+ 1    24     5
+ 2    55     1
+ 2    67     5
+ 2    24     3
+ ", header=TRUE)
> xtabs(Rating ~ User+Movie, data=inp)
    Movie
User 24 34 55 67
   1  5  4  3  0
   2  3  0  1  5

respondido 10 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.