[R] 3D matrix columns messed up

Jim Lemon drjimlemon at gmail.com
Mon Oct 12 12:22:59 CEST 2015


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.glover at 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 at 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]]



More information about the R-help mailing list