0

The initial matrix looks like this:

1 A 1
1 B 1
1 C 1
2 A 1
3 A 1
3 C 1

It is necessary to bring to mind:

  A B C
1 1 1 1
2 1 0 0
3 1 0 1

I am using a code like:

for (i in 1:length(t_data$cid)) {
  t_cid <- t_data$cid[i]
  t_val <- t_data$eventAction[i]
  df_events[grep(t_cid, df_events$cid),grep(paste0("^",t_val,"$"), colnames(df_events))] <- 1
  print(i)
}

But we are talking about more than a million rows in the first matrix and a hundred columns in the second. About 10k rows in 5 mins, too slow. Please help.

  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Take a look at this https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Ronak Shah Mar 26 '21 at 04:29

1 Answers1

6

I think xtabs will help

> as.data.frame.matrix(xtabs(V3 ~ ., df))
  A B C
1 1 1 1
2 1 0 0
3 1 0 1

Data

> dput(df)
structure(list(V1 = c(1L, 1L, 1L, 2L, 3L, 3L), V2 = c("A", "B", 
"C", "A", "A", "C"), V3 = c(1L, 1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA,   
-6L))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81