0

I am struggling to figure out how to develop a square matrix given a format like

user_id book_id rating
3234 1234567892 5
5567 0000252564 7

To something like:

book/user 3234 5567
1234567892 5 0
0000252564 0 7

in R. i tried to use 'Matrix' method but my data frame is too big (1,000,000 records). how can i do that?

IYAR
  • 19
  • 3
  • `out <- reshape::cast(df,book_id~user_id,value="rating")` . Then `out[is.na(out)] <- 0` – maydin Jun 10 '21 at 07:42
  • `reshape2::dcast(df,book_id~user_id,value.var = "rating",fill = 0)` – Onyambu Jun 10 '21 at 07:43
  • How many distinct values do `user_id` and `book_id` have? If there are too many, it will not be easily possible as the result could get very big. Otherwise just use `reshape::cast` or `tidyr::pivot_wider` – AEF Jun 10 '21 at 07:43

0 Answers0