Hello, I have a data.frame with repeating rows and corresponding value. For instance, "z" will be an example of that. x = c("a","a", "a", "b","b","b") y = c(1.0, 1.2, 1.1, 1.01, 1.03, 1.0) z = as.data.frame(cbind(x,y))> zx y 1 a 1 2 a 1.2 3 a 1.1 4 b 1.01 5 b 1.03 6 b 1 So, you see that "a" and "b" are repeated 3 times and have three different value. I would like to convert this data into something like the following. a b 1.0 1.01 1.2 1.03 1.1 1.00 In the above, repeating rows (a,b) become columns and their values show up in their respective column. Finally, to clarify few things. The number of rows of each repeating item (a or b) would be the same and hence, the number of row expected in the output shall be the same. [[alternative HTML version deleted]]
Hi, May be: z<-data.frame(x,y,stringsAsFactors=FALSE) ?simplify2array(split(z[,-1],z$x)) #?????? a??? b #[1,] 1.0 1.01 #[2,] 1.2 1.03 #[3,] 1.1 1.00 ?as.data.frame(simplify2array(split(z[,-1],z$x))) A.K. ----- Original Message ----- From: Brijesh Gulati <brijgul at gmail.com> To: r-help at r-project.org Cc: Sent: Sunday, August 4, 2013 8:49 AM Subject: [R] question: data.frame data conversion Hello, I have a data.frame with repeating rows and corresponding value. For instance, "z" will be an example of that. ? x = c("a","a", "a", "b","b","b") ? y = c(1.0, 1.2, 1.1, 1.01, 1.03, 1.0) ? z = as.data.frame(cbind(x,y))> z? x? ? y 1 a? ? 1 2 a? 1.2 3 a? 1.1 4 b 1.01 5 b 1.03 6 b? ? 1 So, you see that "a" and "b" are repeated 3 times and have three different value. I would like to convert this data into something like the following. ? a? ? b ? 1.0 1.01 ? 1.2 1.03 ? 1.1 1.00 In the above, repeating rows (a,b) become columns and their values show up in their respective column. Finally, to clarify few things. The number of rows of each repeating item (a or b) would be the same and hence, the number of row expected in the output shall be the same. ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Hello, First of all, do _not_ create a data frame with as.data.frame(cbind(...)) Instead, use z = data.frame(x, y) As for your question, try the following. library(reshape2) fun <- function(z){ zs <- split(z, x) n <- length(zs) m <- nrow(zs[[1]]) zz <- cbind(id = rep(1:m, n), z) dcast(zz, id ~ x)[-1] } fun(z) Hope this helps, Rui Barradas Em 04-08-2013 13:49, Brijesh Gulati escreveu:> Hello, I have a data.frame with repeating rows and corresponding value. For > instance, "z" will be an example of that. > > > > x = c("a","a", "a", "b","b","b") > > y = c(1.0, 1.2, 1.1, 1.01, 1.03, 1.0) > > z = as.data.frame(cbind(x,y)) > > > >> z > > x y > > 1 a 1 > > 2 a 1.2 > > 3 a 1.1 > > 4 b 1.01 > > 5 b 1.03 > > 6 b 1 > > > > So, you see that "a" and "b" are repeated 3 times and have three different > value. I would like to convert this data into something like the following. > > > > a b > 1.0 1.01 > 1.2 1.03 > 1.1 1.00 > > > > In the above, repeating rows (a,b) become columns and their values show up > in their respective column. > > Finally, to clarify few things. The number of rows of each repeating item > (a or b) would be the same and hence, the number of row expected in the > output shall be the same. > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >