Hi Jim, Thank you very much for the message. Sorry for the email that was not clear. Yes, you are right, in A$date, should be t2> ENM, t3> ENL. but using the code it gave t2>ENL, t3>ENM. I am struggling to fix it. If the data set was small, I could do it manually.
would you mind to try this example? Here is the example A<-structure(list(Tag = structure(c(1L, 1L, 1L), .Label = "a1", class = "factor"), site = structure(1:3, .Label = c("2C7", "ENL", "ENM"), class = "factor"), DATE = structure(c(1L, 3L, 2L), .Label = c("t1", "t2", "t3" ), class = "factor"), date = structure(c(1L, 3L, 2L), .Label = c("t1", "t2", "t3"), class = "factor")), .Names = c("Tag", "site", "DATE", "date"), row.names = c(NA, -3L), class = "data.frame") A$date<-factor(A$DATE, levels=c("t1","t2","t3")) tmp <- split(A, A$Tag) head(tmp) tail(tmp) tmp1 <- do.call(rbind, lapply(tmp, function(x){ tb <- table(A$date) idx <- which(tb>0) tb1 <- replace(tb, idx, as.character(A$site)) })) tmp1 ========= ________________________________ From: Jim Lemon <drjimle...@gmail.com> Sent: October 13, 2015 4:24 AM To: Kristi Glover Cc: R-help Subject: Re: [R] 3D matrix columns messed up _ looking for your help Hi Kristi, This is a bit hard to follow, but I'll try. As you are replacing the numeric values of the intermediate table with the character values of the factor A$date, it looks to me as though the answer is as it should be. 2 -> ENL, 3 -> ENM. I suspect that the solution is not difficult, but I can't quite make out what you are trying to accomplish. Jim On Tue, Oct 13, 2015 at 11:44 AM, Kristi Glover <kristi.glo...@hotmail.com<mailto:kristi.glo...@hotmail.com>> wrote: Hi Jim, Thank you very much for your suggestions. It seems very easy but it is frustrating as it did not work me. with creating factors and rearranging the columns, still z value (site) did change. for example Tag site DATE a1 2C7 t1 a1 ENL t3 a1 ENM t2 ENL is supposed to be assigned for the period t3. ENM should be assigned in t2, but using the code, the table gave wrong information as the z value (site) did not move to the corresponding column. ENL is in t2, ENM is inn t3 coumns, which is wrong. > tmp1 t1 t2 t3 a1 "2C7" "ENL" "ENM" I have included the code if any one help me to solve the problem. This is a just example, I have a very big data set so that I could not check it manually therefore, I just checked few rows but it did not work. Your help is highly appreciated. Here is the example A<-structure(list(Tag = structure(c(1L, 1L, 1L), .Label = "a1", class = "factor"), site = structure(1:3, .Label = c("2C7", "ENL", "ENM"), class = "factor"), DATE = structure(c(1L, 3L, 2L), .Label = c("t1", "t2", "t3" ), class = "factor"), date = structure(c(1L, 3L, 2L), .Label = c("t1", "t2", "t3"), class = "factor")), .Names = c("Tag", "site", "DATE", "date"), row.names = c(NA, -3L), class = "data.frame") A$date<-factor(A$DATE, levels=c("t1","t2","t3")) tmp <- split(A, A$Tag) head(tmp) tail(tmp) tmp1 <- do.call(rbind, lapply(tmp, function(x){ tb <- table(A$date) idx <- which(tb>0) tb1 <- replace(tb, idx, as.character(A$site)) })) tmp1 ________________________________ From: Jim Lemon <drjimle...@gmail.com<mailto:drjimle...@gmail.com>> Sent: October 12, 2015 4:22 AM To: Kristi Glover Cc: R-help Subject: Re: [R] 3D matrix columns messed up Hi Kristi, The first part is relatively easy: # change first line to x$time<-factor(x$time,levels=c("t1","t2","t3","t4","t10","t21")) As you have specified "site" as the second element in "x", not the third, perhaps you just want: x<-structure(list(vs = structure(c(1L, 1L, 2L, 3L, 4L, 2L, 3L, 1L, 1L), .Label = c("vs1", "vs2", "vs3", "vs4"), class = "factor"), time = structure(c(1L, 3L, 5L, 1L, 5L, 1L, 6L, 2L, 4L), .Label = c("t1", "t10", "t2", "t21", "t3", "t4"), class = "factor")), site = structure(c(1L, 2L, 3L, 1L, 3L, 1L, 3L, 1L, 2L), .Label = c("A", "B", "D"), class = "factor"), .Names = c("vs", "time", "site"), class = "data.frame", row.names = c(NA, -9L)) Jim On Mon, Oct 12, 2015 at 7:41 PM, Kristi Glover <kristi.glo...@hotmail.com<mailto:kristi.glo...@hotmail.com>> wrote: 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. ______________________________________________ R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.