简体   繁体   中英

Fill a matrix with a value from another matrix in R

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM