Re: [R] reformatting some data
On Dec 4, 2012, at 1:44 PM, arun wrote: Hi, You can also do this: dat1<-structure(list(group = c(4L, 3L, 4L, 4L, 4L, 2L), X3.Hydroxybutyrate = c(4e-04, 5e-04, 4e-04, 6e-04, 5e-04, 7e-04), X3.Hydroxyisovalerate = c(3e-04, 3e-04, 3e-04, 3e-04, 3e-04, 4e-04), ADP = c(5e-04, 6e-04, 6e-04, 5e-04, 7e-04, 7e-04)), .Names = c("group", "X3.Hydroxybutyrate", "X3.Hydroxyisovalerate", "ADP"), row.names = c(347L, 353L, 359L, 365L, 371L, 377L), class = "data.frame") datM<-melt(dat1,id.var="group") An explicit call to library(reshape2) might be in order before a melt call. xtabs(value~variable+group,data=datM) # group #variable 2 3 4 # X3.Hydroxybutyrate0.0007 0.0005 0.0019 # X3.Hydroxyisovalerate 0.0004 0.0003 0.0012 # ADP 0.0007 0.0006 0.0023 This has implicitly summed the entires from items by "group" values. That might or might not have been what the OP wanted. xtabs does not allow a function to be specified. (It's output is expected to be a contingency table of counts so it only makes sense to sum values.) The aggregate function allows either choice within the base package: I see that arun used `mean` as his function in an earlier post using reshape2::dcast > aggregate(dat1[-1] , dat1['group'], FUN=mean) group X3.Hydroxybutyrate X3.Hydroxyisovalerate ADP 1 2 0.000700 4e-04 0.000700 2 3 0.000500 3e-04 0.000600 3 4 0.000475 3e-04 0.000575 This is the aggregate method for summation: > aggregate(dat1[-1] , dat1['group'], FUN=sum) group X3.Hydroxybutyrate X3.HydroxyisovalerateADP 1 2 0.00070.0004 0.0007 2 3 0.00050.0003 0.0006 3 4 0.00190.0012 0.0023 A.K. - Original Message - From: Charles Determan Jr To: r-help@r-project.org Cc: Sent: Tuesday, December 4, 2012 4:17 PM Subject: [R] reformatting some data Hello, I am trying to reformat some data so that it is organized by group in the columns. The data currently looks like this: group X3.Hydroxybutyrate X3.Hydroxyisovalerate ADP 347 4 4e-04 3e-04 5e-04 353 3 5e-04 3e-04 6e-04 359 4 4e-04 3e-04 6e-04 365 4 6e-04 3e-04 5e-04 371 4 5e-04 3e-04 7e-04 377 2 7e-04 4e-04 7e-04 I would like to reformat it so it is like this: 2 3 4 var1 var2 var3 I realize that there unequal numbers in each group but I would like to none-the-less if possible. Here is a subset of the data: structure(list(group = c(4L, 3L, 4L, 4L, 4L, 2L), X3.Hydroxybutyrate = c(4e-04, 5e-04, 4e-04, 6e-04, 5e-04, 7e-04), X3.Hydroxyisovalerate = c(3e-04, 3e-04, 3e-04, 3e-04, 3e-04, 4e-04), ADP = c(5e-04, 6e-04, 6e-04, 5e-04, 7e-04, 7e-04)), .Names = c("group", "X3.Hydroxybutyrate", "X3.Hydroxyisovalerate", "ADP"), row.names = c(347L, 353L, 359L, 365L, 371L, 377L), class = "data.frame") David Winsemius, MD Alameda, CA, USA __ 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.
Re: [R] reformatting some data
Hi, You can also do this: dat1<-structure(list(group = c(4L, 3L, 4L, 4L, 4L, 2L), X3.Hydroxybutyrate = c(4e-04, 5e-04, 4e-04, 6e-04, 5e-04, 7e-04), X3.Hydroxyisovalerate = c(3e-04, 3e-04, 3e-04, 3e-04, 3e-04, 4e-04), ADP = c(5e-04, 6e-04, 6e-04, 5e-04, 7e-04, 7e-04)), .Names = c("group", "X3.Hydroxybutyrate", "X3.Hydroxyisovalerate", "ADP"), row.names = c(347L, 353L, 359L, 365L, 371L, 377L), class = "data.frame") datM<-melt(dat1,id.var="group") xtabs(value~variable+group,data=datM) # group #variable 2 3 4 # X3.Hydroxybutyrate 0.0007 0.0005 0.0019 # X3.Hydroxyisovalerate 0.0004 0.0003 0.0012 # ADP 0.0007 0.0006 0.0023 A.K. - Original Message - From: Charles Determan Jr To: r-help@r-project.org Cc: Sent: Tuesday, December 4, 2012 4:17 PM Subject: [R] reformatting some data Hello, I am trying to reformat some data so that it is organized by group in the columns. The data currently looks like this: group X3.Hydroxybutyrate X3.Hydroxyisovalerate ADP 347 4 4e-04 3e-04 5e-04 353 3 5e-04 3e-04 6e-04 359 4 4e-04 3e-04 6e-04 365 4 6e-04 3e-04 5e-04 371 4 5e-04 3e-04 7e-04 377 2 7e-04 4e-04 7e-04 I would like to reformat it so it is like this: 2 3 4 var1 var2 var3 I realize that there unequal numbers in each group but I would like to none-the-less if possible. Here is a subset of the data: structure(list(group = c(4L, 3L, 4L, 4L, 4L, 2L), X3.Hydroxybutyrate = c(4e-04, 5e-04, 4e-04, 6e-04, 5e-04, 7e-04), X3.Hydroxyisovalerate = c(3e-04, 3e-04, 3e-04, 3e-04, 3e-04, 4e-04), ADP = c(5e-04, 6e-04, 6e-04, 5e-04, 7e-04, 7e-04)), .Names = c("group", "X3.Hydroxybutyrate", "X3.Hydroxyisovalerate", "ADP"), row.names = c(347L, 353L, 359L, 365L, 371L, 377L), class = "data.frame") Any insight is truly appreciated, Regards, Charles [[alternative HTML version deleted]] __ 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.
Re: [R] reformatting some data
Hi, Not sure whether this helps: library(reshape2) dat1<-structure(list(group = c(4L, 3L, 4L, 4L, 4L, 2L), X3.Hydroxybutyrate = c(4e-04, 5e-04, 4e-04, 6e-04, 5e-04, 7e-04), X3.Hydroxyisovalerate = c(3e-04, 3e-04, 3e-04, 3e-04, 3e-04, 4e-04), ADP = c(5e-04, 6e-04, 6e-04, 5e-04, 7e-04, 7e-04)), .Names = c("group", "X3.Hydroxybutyrate", "X3.Hydroxyisovalerate", "ADP"), row.names = c(347L, 353L, 359L, 365L, 371L, 377L), class = "data.frame") datM<-melt(dat1,id.var="group") dcast(datM,variable~group,length) #or dcast(datM,variable~group,mean) # variable 2 3 4 #1 X3.Hydroxybutyrate 7e-04 5e-04 0.000475 #2 X3.Hydroxyisovalerate 4e-04 3e-04 0.000300 #3 ADP 7e-04 6e-04 0.000575 A.K> - Original Message - From: Charles Determan Jr To: r-help@r-project.org Cc: Sent: Tuesday, December 4, 2012 4:17 PM Subject: [R] reformatting some data Hello, I am trying to reformat some data so that it is organized by group in the columns. The data currently looks like this: group X3.Hydroxybutyrate X3.Hydroxyisovalerate ADP 347 4 4e-04 3e-04 5e-04 353 3 5e-04 3e-04 6e-04 359 4 4e-04 3e-04 6e-04 365 4 6e-04 3e-04 5e-04 371 4 5e-04 3e-04 7e-04 377 2 7e-04 4e-04 7e-04 I would like to reformat it so it is like this: 2 3 4 var1 var2 var3 I realize that there unequal numbers in each group but I would like to none-the-less if possible. Here is a subset of the data: structure(list(group = c(4L, 3L, 4L, 4L, 4L, 2L), X3.Hydroxybutyrate = c(4e-04, 5e-04, 4e-04, 6e-04, 5e-04, 7e-04), X3.Hydroxyisovalerate = c(3e-04, 3e-04, 3e-04, 3e-04, 3e-04, 4e-04), ADP = c(5e-04, 6e-04, 6e-04, 5e-04, 7e-04, 7e-04)), .Names = c("group", "X3.Hydroxybutyrate", "X3.Hydroxyisovalerate", "ADP"), row.names = c(347L, 353L, 359L, 365L, 371L, 377L), class = "data.frame") Any insight is truly appreciated, Regards, Charles [[alternative HTML version deleted]] __ 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.
Re: [R] reformatting some data
That should read the "reshape" package -- not the "shape" package. My apologies. To: Charles Determan Jr ; "r-help@r-project.org" Sent: Tuesday, December 4, 2012 4:36 PM Subject: Re: [R] reformatting some data It's not clear to me what it is you are attempting to do, as you switch from a very specific example to some general example with the vague terms "var1" "var2", and "var3." It sounds like you might be trying to do something similar to what would be available in the shape package using the melt function. Try ?melt.data.frame. --Mark Lamias From: Charles Determan Jr To: r-help@r-project.org Sent: Tuesday, December 4, 2012 4:17 PM Subject: [R] reformatting some data Hello, I am trying to reformat some data so that it is organized by group in the columns. The data currently looks like this: group X3.Hydroxybutyrate X3.Hydroxyisovalerate ADP 347 4 4e-04 3e-04 5e-04 353 3 5e-04 3e-04 6e-04 359 4 4e-04 3e-04 6e-04 365 4 6e-04 3e-04 5e-04 371 4 5e-04 3e-04 7e-04 377 2 7e-04 4e-04 7e-04 I would like to reformat it so it is like this: 2 3 4 var1 var2 var3 I realize that there unequal numbers in each group but I would like to none-the-less if possible. Here is a subset of the data: structure(list(group = c(4L, 3L, 4L, 4L, 4L, 2L), X3.Hydroxybutyrate = c(4e-04, 5e-04, 4e-04, 6e-04, 5e-04, 7e-04), X3.Hydroxyisovalerate = c(3e-04, 3e-04, 3e-04, 3e-04, 3e-04, 4e-04), ADP = c(5e-04, 6e-04, 6e-04, 5e-04, 7e-04, 7e-04)), .Names = c("group", "X3.Hydroxybutyrate", "X3.Hydroxyisovalerate", "ADP"), row.names = c(347L, 353L, 359L, 365L, 371L, 377L), class = "data.frame") Any insight is truly appreciated, Regards, Charles [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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.
Re: [R] reformatting some data
It's not clear to me what it is you are attempting to do, as you switch from a very specific example to some general example with the vague terms "var1" "var2", and "var3." It sounds like you might be trying to do something similar to what would be available in the shape package using the melt function. Try ?melt.data.frame. --Mark Lamias From: Charles Determan Jr To: r-help@r-project.org Sent: Tuesday, December 4, 2012 4:17 PM Subject: [R] reformatting some data Hello, I am trying to reformat some data so that it is organized by group in the columns. The data currently looks like this: group X3.Hydroxybutyrate X3.Hydroxyisovalerate ADP 347 4 4e-04 3e-04 5e-04 353 3 5e-04 3e-04 6e-04 359 4 4e-04 3e-04 6e-04 365 4 6e-04 3e-04 5e-04 371 4 5e-04 3e-04 7e-04 377 2 7e-04 4e-04 7e-04 I would like to reformat it so it is like this: 2 3 4 var1 var2 var3 I realize that there unequal numbers in each group but I would like to none-the-less if possible. Here is a subset of the data: structure(list(group = c(4L, 3L, 4L, 4L, 4L, 2L), X3.Hydroxybutyrate = c(4e-04, 5e-04, 4e-04, 6e-04, 5e-04, 7e-04), X3.Hydroxyisovalerate = c(3e-04, 3e-04, 3e-04, 3e-04, 3e-04, 4e-04), ADP = c(5e-04, 6e-04, 6e-04, 5e-04, 7e-04, 7e-04)), .Names = c("group", "X3.Hydroxybutyrate", "X3.Hydroxyisovalerate", "ADP"), row.names = c(347L, 353L, 359L, 365L, 371L, 377L), class = "data.frame") Any insight is truly appreciated, Regards, Charles [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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.