[R] 3D matrix columns messed up

Kristi Glover kristi.glover at hotmail.com
Mon Oct 12 10:41:13 CEST 2015


Hi R Users,
I was trying to make a matrix with three variables (x,y, z), but y variable (columns) names did not stay in its sequential order, t1,t2,t3,---t21; rather the matrix columns automatically appeared as a t1,t10, t2,t20 etc. Besides these, z value (sites) did not come in the right place (meaning in right columns name). I am wondering how I can make the matrix with the sequential order with right z value (site). I tried it several ways but did not work. One of the examples I used is given here. Would you mind to give me a mints? 

x<-structure(list(vs = structure(c(1L, 1L, 2L, 3L, 4L, 2L, 3L, 1L, 
1L), .Label = c("vs1", "vs2", "vs3", "vs4"), class = "factor"), 
    site = structure(c(1L, 2L, 3L, 1L, 3L, 1L, 3L, 1L, 2L), .Label = c("A", 
    "B", "D"), class = "factor"), time = structure(c(1L, 3L, 
    5L, 1L, 5L, 1L, 6L, 2L, 4L), .Label = c("t1", "t10", "t2", 
    "t21", "t3", "t4"), class = "factor")), .Names = c("vs", 
"site", "time"), class = "data.frame", row.names = c(NA, -9L))


x$time<-factor(x$time)
tmp <- split(x, x$vs)
tmp1 <- do.call(rbind, lapply(tmp, function(x){
tb <- table(x$time)
idx <- which(tb>0)
tb1 <- replace(tb, idx, as.character(x$site))
}))


tmp1


## I want the z (site) in respective columns. 



More information about the R-help mailing list