Suppose we have a matrix mwith 3 columns and a vector idwith identification for the rows:
m <- matrix(c(1,1,2,1,2,3,2,2,2,3,3,4,6,7,7,
2,2,2,4,4,5,8,9,9),ncol=3,byrow=T)
id <- c(1,2,3,4,5,1,4,5)
What is the fastest way to extract strings from musing id?
As a result, I would like to have a vector for each unique identifier in id. Sort of:
#
#
#
#
#
My pretty bad solution is too slow for my purposes:
pts_list <- list()
for (i in unique(id)){
pts_list[[i]] <- as.vector(t(m[id==i,]))
}
pts_list
Here's a little script for checking speed (this is really ugly ...):
pts_list <- list()
m2 <- cbind(m,m,m,m)
m3 <- rbind(m2,m2,m2,m2,m2,m2,m2,m2,m2,m2)
m4 <- rbind(m3,m3,m3,m3,m3,m3,m3,m3,m3,m3)
m5 <- rbind(m4,m4,m4,m4,m4,m4,m4,m4,m4,m4)
m6 <- rbind(m5,m5,m5,m5,m5,m5,m5,m5,m5,m5)
id6 <- rep(1:8000,10)
system.time(
for (i in unique(id6)){
pts_list[[i]] <- as.vector(t(m6[id6==i,]))
}
)
Any suggestions?