Dear R-users, This should be simple but still eludes me: Given the following tmp<-as.data.frame(matrix(c(44, 10, "abc", 1, 44, 10, "def", 1, 44, 12, "abc", 2), 3, 4, byrow=T)) I want to expand the data to the following form: V1 V2 V3 V4 1 44 10 abc 1 2 44 10 def 1 3 44 12 abc 1 4 44 12 abc 1 The last row of the original df was duplicated the row by the number in the 4th column (which could be expendable being all ones) I clumsily tried a few variants of a loop but I am not making any progress. Any hints would be greatly appreciated. for (i in 1:3){ rbind(rep(tmp[i,], temp[i,4]) } -- View this message in context: http://r.789695.n4.nabble.com/Simple-but-elusive-expand-back-from-counts-tp3415727p3415727.html Sent from the R help mailing list archive at Nabble.com.
Hi, You can use rep() to repeat the appropriate rows as in V4. For example: tmp[rep(rownames(tmp), tmp$V4), ] V1 V2 V3 V4 1 44 10 abc 1 2 44 10 def 1 3 44 12 abc 2 3.1 44 12 abc 2 if you wanted, you could then reset the row names to NULL to get 1, 2, 3, 4 rather than 3, 3.1 rownames(tmp) <- NULL ## AFTER saving the expanded form. Cheers, Josh On Tue, Mar 29, 2011 at 11:15 AM, jjap <sabjap at gmail.com> wrote:> Dear R-users, > > This should be simple but still eludes me: > > Given the following > tmp<-as.data.frame(matrix(c(44, 10, "abc", 1, 44, 10, "def", 1, 44, 12, > "abc", 2), 3, 4, byrow=T)) > > I want to ?expand the data to the following form: > > ?V1 V2 ?V3 V4 > 1 44 10 abc ?1 > 2 44 10 def ?1 > 3 44 12 abc ?1 > 4 44 12 abc ?1 > > The last row of the original df was duplicated the row by the number in the > 4th column (which could be expendable being all ones) > I clumsily tried a few variants of a loop but I am not making any progress. > Any hints would be greatly appreciated. > for (i in 1:3){ > ?rbind(rep(tmp[i,], temp[i,4]) > ?} > > -- > View this message in context: http://r.789695.n4.nabble.com/Simple-but-elusive-expand-back-from-counts-tp3415727p3415727.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Tena koe Try something like: tmp[rep(1:nrow(tmp), each=tmp[,4]),] # untested HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of jjap > Sent: Wednesday, 30 March 2011 7:16 a.m. > To: r-help at r-project.org > Subject: [R] Simple but elusive - expand back from counts > > Dear R-users, > > This should be simple but still eludes me: > > Given the following > tmp<-as.data.frame(matrix(c(44, 10, "abc", 1, 44, 10, "def", 1, 44, 12, > "abc", 2), 3, 4, byrow=T)) > > I want to expand the data to the following form: > > V1 V2 V3 V4 > 1 44 10 abc 1 > 2 44 10 def 1 > 3 44 12 abc 1 > 4 44 12 abc 1 > > The last row of the original df was duplicated the row by the number in > the > 4th column (which could be expendable being all ones) > I clumsily tried a few variants of a loop but I am not making any > progress. > Any hints would be greatly appreciated. > for (i in 1:3){ > rbind(rep(tmp[i,], temp[i,4]) > } > > -- > View this message in context: http://r.789695.n4.nabble.com/Simple-but- > elusive-expand-back-from-counts-tp3415727p3415727.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
On Tue, Mar 29, 2011 at 11:15:33AM -0700, jjap wrote:> Dear R-users, > > This should be simple but still eludes me: > > Given the following > tmp<-as.data.frame(matrix(c(44, 10, "abc", 1, 44, 10, "def", 1, 44, 12, > "abc", 2), 3, 4, byrow=T)) > > I want to expand the data to the following form: > > V1 V2 V3 V4 > 1 44 10 abc 1 > 2 44 10 def 1 > 3 44 12 abc 1 > 4 44 12 abc 1 > > The last row of the original df was duplicated the row by the number in the > 4th column (which could be expendable being all ones)Hi. Try the following. tmp1 <- tmp[rep(1:nrow(tmp), times=tmp$V4), ] tmp1$V4 <- 1 row.names(tmp1) <- NULL tmp1 V1 V2 V3 V4 1 44 10 abc 1 2 44 10 def 1 3 44 12 abc 1 4 44 12 abc 1 Hope this helps. Petr Savicky.
Henrique Dallazuanna
2011-Mar-29 19:19 UTC
[R] Simple but elusive - expand back from counts
Try this: transform(tmp[rep(seq(nrow(tmp)), as.numeric(tmp$V4)),], V4 = 1) On Tue, Mar 29, 2011 at 3:15 PM, jjap <sabjap at gmail.com> wrote:> Dear R-users, > > This should be simple but still eludes me: > > Given the following > tmp<-as.data.frame(matrix(c(44, 10, "abc", 1, 44, 10, "def", 1, 44, 12, > "abc", 2), 3, 4, byrow=T)) > > I want to ?expand the data to the following form: > > ?V1 V2 ?V3 V4 > 1 44 10 abc ?1 > 2 44 10 def ?1 > 3 44 12 abc ?1 > 4 44 12 abc ?1 > > The last row of the original df was duplicated the row by the number in the > 4th column (which could be expendable being all ones) > I clumsily tried a few variants of a loop but I am not making any progress. > Any hints would be greatly appreciated. > for (i in 1:3){ > ?rbind(rep(tmp[i,], temp[i,4]) > ?} > > -- > View this message in context: http://r.789695.n4.nabble.com/Simple-but-elusive-expand-back-from-counts-tp3415727p3415727.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O