Hi, If you do not want the change the row order or column order, ddply() may be helpful: library(plyr) new1<-ddply(x,.(x1,x2),function(x) x$value) unlist(list(new1$V1,new1$V2)) #[1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819 0.8983897 0.9446753 #[8] 0.6607978 A.K.
----- Original Message ----- From: Marius Hofert <marius.hof...@math.ethz.ch> To: R-help <r-h...@stat.math.ethz.ch> Cc: Sent: Saturday, September 15, 2012 2:07 PM Subject: [R] How to convert the output of tapply() so that it has the same order as the input? Hi, I try to apply a function to subsets of a data.frame. tapply() does the job, but the as output, I am looking for a vector (not an array/matrix) ordered in the same way as the original data, so I can simply cbind the result to the original data.frame. Below is a minimal example. I know that there are packages that can do these things easier, but I'm looking for a fast solution not requiring additional packages. Cheers, Marius ## data.frame set.seed(1) (x <- data.frame(x1=LETTERS[1:4], x2=1:2, value=runif(8))) ## apply a function to each subset combination of x1 and x2 y <- tapply(x$value, x[,-3], function(x) x) ## (trials of) transforming the output to be of the same type and order as x$value (y. <- do.call(rbind, y)) # => wrong order (y. <- do.call(cbind, y)) # => wrong order ## appending (that's the goal) z <- x z$value <- y. ______________________________________________ R-help@r-project.org mailing list 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. ______________________________________________ R-help@r-project.org mailing list 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.