[R] reshaping a data frame
Hi Guys I am trying to cast a data frame but not aggregate the rows for the same variable. here is a contrived example. **input** temp_df - data.frame(names=c('foo','foo','foo'),variable=c('w','w','w'),value=c(34,65,12)) temp_df names variable value 1 foow34 2 foow65 3 foow12 ### **Want this** names w foo 34 foo 65 foo 12 ## **getting this*** ## cast(temp_df) Aggregation requires fun.aggregate: length used as default names w 1 foo 3 In real dataset the categorical column 'variable' will have many more categorical variable. Thanks! -Abhi __ 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] reshaping a data frame
Abhi, In the example you give, you don't really need to reshape the data ... just rename the column value to w. Here's a different example with more than one category ... tempdf - expand.grid(names=c(foo, bar), variable=letters[1:3]) tempdf$value - rnorm(dim(tempdf)[1]) tempdf library(reshape) cast(tempdf) But, that may not be what you want, If not, please give an example with more than one category showing us what you have and what you want. Jean On Mon, Jun 10, 2013 at 1:15 AM, Abhishek Pratap abhishek@gmail.comwrote: Hi Guys I am trying to cast a data frame but not aggregate the rows for the same variable. here is a contrived example. **input** temp_df - data.frame(names=c('foo','foo','foo'),variable=c('w','w','w'),value=c(34,65,12)) temp_df names variable value 1 foow34 2 foow65 3 foow12 ### **Want this** names w foo 34 foo 65 foo 12 ## **getting this*** ## cast(temp_df) Aggregation requires fun.aggregate: length used as default names w 1 foo 3 In real dataset the categorical column 'variable' will have many more categorical variable. Thanks! -Abhi __ 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] reshaping a data frame
Unless I completely misunderstand what you are doing you don't need to aggregate, just drop the one column and rename things newtemp - temp_df[, c(1,3)] names(newtemp) - c(names, w) newtemp John Kane Kingston ON Canada -Original Message- From: abhishek@gmail.com Sent: Sun, 9 Jun 2013 23:15:48 -0700 To: r-help@r-project.org Subject: [R] reshaping a data frame Hi Guys I am trying to cast a data frame but not aggregate the rows for the same variable. here is a contrived example. **input** temp_df - data.frame(names=c('foo','foo','foo'),variable=c('w','w','w'),value=c(34,65,12)) temp_df names variable value 1 foow34 2 foow65 3 foow12 ### **Want this** names w foo 34 foo 65 foo 12 ## **getting this*** ## cast(temp_df) Aggregation requires fun.aggregate: length used as default names w 1 foo 3 In real dataset the categorical column 'variable' will have many more categorical variable. Thanks! -Abhi __ 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. FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop! __ 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] reshaping a data frame
Hi,If your dataset is similar to the one below: set.seed(24) temp1_df- data.frame(names=rep(c('foo','foo1'),each=6),variable=rep(c('w','x'),times=6),value=sample(25:40,12,replace=TRUE),stringsAsFactors=FALSE) library(reshape2) res-dcast(within(temp1_df,{Seq1-ave(value,names,variable,FUN=seq_along)}),names+Seq1~variable,value.var=value)[,-2] res # names w x #1 foo 29 28 #2 foo 36 33 #3 foo 35 39 #4 foo1 29 37 #5 foo1 37 29 #6 foo1 34 30 A.K. - Original Message - From: Abhishek Pratap abhishek@gmail.com To: r-help@r-project.org r-help@r-project.org Cc: Sent: Monday, June 10, 2013 2:15 AM Subject: [R] reshaping a data frame Hi Guys I am trying to cast a data frame but not aggregate the rows for the same variable. here is a contrived example. **input** temp_df - data.frame(names=c('foo','foo','foo'),variable=c('w','w','w'),value=c(34,65,12)) temp_df names variable value 1 foo w 34 2 foo w 65 3 foo w 12 ### **Want this** names w foo 34 foo 65 foo 12 ## **getting this*** ## cast(temp_df) Aggregation requires fun.aggregate: length used as default names w 1 foo 3 In real dataset the categorical column 'variable' will have many more categorical variable. Thanks! -Abhi __ 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] reshaping a data frame
Thanks everyone for your quick reply. I think my contrived example hid the complexity I wanted to show by using only one variable. @Arun: I think your example is exactly what I was looking for. Very cool trick with 'ave' and 'seq_along'...just dint occur to me. Best, -Abh On Mon, Jun 10, 2013 at 7:13 AM, arun smartpink...@yahoo.com wrote: Hi,If your dataset is similar to the one below: set.seed(24) temp1_df- data.frame(names=rep(c('foo','foo1'),each=6),variable=rep(c('w','x'),times=6),value=sample(25:40,12,replace=TRUE),stringsAsFactors=FALSE) library(reshape2) res-dcast(within(temp1_df,{Seq1-ave(value,names,variable,FUN=seq_along)}),names+Seq1~variable,value.var=value)[,-2] res # names w x #1 foo 29 28 #2 foo 36 33 #3 foo 35 39 #4 foo1 29 37 #5 foo1 37 29 #6 foo1 34 30 A.K. - Original Message - From: Abhishek Pratap abhishek@gmail.com To: r-help@r-project.org r-help@r-project.org Cc: Sent: Monday, June 10, 2013 2:15 AM Subject: [R] reshaping a data frame Hi Guys I am trying to cast a data frame but not aggregate the rows for the same variable. here is a contrived example. **input** temp_df - data.frame(names=c('foo','foo','foo'),variable=c('w','w','w'),value=c(34,65,12)) temp_df names variable value 1 foow34 2 foow65 3 foow12 ### **Want this** names w foo 34 foo 65 foo 12 ## **getting this*** ## cast(temp_df) Aggregation requires fun.aggregate: length used as default names w 1 foo 3 In real dataset the categorical column 'variable' will have many more categorical variable. Thanks! -Abhi __ 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] Reshaping a data frame with a series of factors and 23 repeated measures
Ista, I have looked at the reshape package and have used Âmelt¹ successfully on simpler tables. I tried it here, but have not been successful. I think I just need to gain experience. I am loving R and am having a difficult time with data structure issues. I am attaching the data set that I am trying to manipulate. Ultimately, I would like to be able to analyze these data with ANOVA and repeated measures, and also be able to plot growth, wt * days. I appreciate any help or guidance on references to read that will help me solve my problems. The data set represents wt over time (starting with days=0, birth day) for steers. Factors include Stockering and Finishing treatments. Regards, Bill On 3/29/10 3:39 PM, Ista Zahn [via R] ml-node+1695531-1043721504-210...@n4.nabble.com wrote: Hi Bill, Without an example dataset it's hard to see exactly what you need to do. But you can get started by looking at the documentation for the reshape function (?reshape), and by looking at the reshape package. The reshape package has an associated web page (http://had.co.nz/reshape/) with links to papers and other information to help you get started. Best, Ista On Mon, Mar 29, 2010 at 3:15 PM, wclapham [hidden email] http://n4.nabble.com/user/SendEmail.jtp?type=nodenode=1695531i=0 http://n4.nabble.com/user/SendEmail.jtp?type=nodeamp;node=1695531amp;i=0 wrote: I have a data frame that I created using read.table on a csv spreadsheet. The data look like the following: Steer.ID  stocker.trt  Finish.trt  Date  Days Wt .. Steer.Id, stocker.trt, Finish.trt are factors-- Date, Days, Wt are data that are repeated 23 times (wide format). I want to reshape the data such that I have the correct Steer.ID, stocker.trt, Finish.trt identifying all of the repeated measures data in a long  format. I am a newbie at R and need to develop the skill in reshaping data, so that I can handle routine problems like described above. Thanks so much in advance for help or advice. Bill -- View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-r epeated-measures-tp1695500p1695500.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] http://n4.nabble.com/user/SendEmail.jtp?type=nodenode=1695531i=1 http://n4.nabble.com/user/SendEmail.jtp?type=nodeamp;node=1695531amp;i=1 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. -- View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-repeated-measures-tp1695500p1745223.html Sent from the R help mailing list archive at Nabble.com. [[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] Reshaping a data frame with a series of factors and 23 repeated measures
Try this: cattle - read.csv(http://n4.nabble.com/attachment/1745223/0/CattleGrowth.csv;) long - reshape(cattle, dir = long, idvar = Steer.ID, varying = list(grep(Date, names(cattle)), grep(Days, names(cattle)), grep(Wt,names(cattle On Tue, Mar 30, 2010 at 9:34 AM, wclapham william.clap...@ars.usda.gov wrote: Ista, I have looked at the reshape package and have used Œmelt¹ successfully on simpler tables. I tried it here, but have not been successful. I think I just need to gain experience. I am loving R and am having a difficult time with data structure issues. I am attaching the data set that I am trying to manipulate. Ultimately, I would like to be able to analyze these data with ANOVA and repeated measures, and also be able to plot growth, wt * days. I appreciate any help or guidance on references to read that will help me solve my problems. The data set represents wt over time (starting with days=0, birth day) for steers. Factors include Stockering and Finishing treatments. Regards, Bill On 3/29/10 3:39 PM, Ista Zahn [via R] ml-node+1695531-1043721504-210...@n4.nabble.com wrote: Hi Bill, Without an example dataset it's hard to see exactly what you need to do. But you can get started by looking at the documentation for the reshape function (?reshape), and by looking at the reshape package. The reshape package has an associated web page (http://had.co.nz/reshape/) with links to papers and other information to help you get started. Best, Ista On Mon, Mar 29, 2010 at 3:15 PM, wclapham [hidden email] http://n4.nabble.com/user/SendEmail.jtp?type=nodenode=1695531i=0 http://n4.nabble.com/user/SendEmail.jtp?type=nodeamp;node=1695531amp;i=0 wrote: I have a data frame that I created using read.table on a csv spreadsheet. The data look like the following: Steer.ID stocker.trt Finish.trt Date Days Wt .. Steer.Id, stocker.trt, Finish.trt are factors-- Date, Days, Wt are data that are repeated 23 times (wide format). I want to reshape the data such that I have the correct Steer.ID, stocker.trt, Finish.trt identifying all of the repeated measures data in a long format. I am a newbie at R and need to develop the skill in reshaping data, so that I can handle routine problems like described above. Thanks so much in advance for help or advice. Bill -- View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-r epeated-measures-tp1695500p1695500.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] http://n4.nabble.com/user/SendEmail.jtp?type=nodenode=1695531i=1 http://n4.nabble.com/user/SendEmail.jtp?type=nodeamp;node=1695531amp;i=1 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. -- View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-repeated-measures-tp1695500p1745223.html Sent from the R help mailing list archive at Nabble.com. [[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] Reshaping a data frame with a series of factors and 23 repeated measures
Hi Bill, Here is a reshape package version. The key thing to notice is that you have multiple pieces of information in your original column names. These can be split out using the colsplit() function: # Read in the data cattle - read.csv(http://n4.nabble.com/attachment/1745223/0/CattleGrowth.csv;, colClasses=character) # make the naming scheme consistent names(cattle)[names(cattle) %in% c(Date, Days, Wt)] - c(Date.0, Days.0, Wt.0) # melt the data m.cattle - melt(cattle, id = c(Steer.ID, stocker.trt, Finish.trt)) # split out variable and time info m.cattle - as.data.frame(cbind(colsplit(m.cattle$variable, split=\\., names=c(Var, time)), m.cattle)) # get rid of the now-redundant variable column m.cattle$variable - NULL # cast the data to put variables back in the columns long.cattle - cast(m.cattle, ... ~ Var) Best, Ista On Tue, Mar 30, 2010 at 9:34 AM, wclapham william.clap...@ars.usda.gov wrote: Ista, I have looked at the reshape package and have used Œmelt¹ successfully on simpler tables. I tried it here, but have not been successful. I think I just need to gain experience. I am loving R and am having a difficult time with data structure issues. I am attaching the data set that I am trying to manipulate. Ultimately, I would like to be able to analyze these data with ANOVA and repeated measures, and also be able to plot growth, wt * days. I appreciate any help or guidance on references to read that will help me solve my problems. The data set represents wt over time (starting with days=0, birth day) for steers. Factors include Stockering and Finishing treatments. Regards, Bill On 3/29/10 3:39 PM, Ista Zahn [via R] ml-node+1695531-1043721504-210...@n4.nabble.com wrote: Hi Bill, Without an example dataset it's hard to see exactly what you need to do. But you can get started by looking at the documentation for the reshape function (?reshape), and by looking at the reshape package. The reshape package has an associated web page (http://had.co.nz/reshape/) with links to papers and other information to help you get started. Best, Ista On Mon, Mar 29, 2010 at 3:15 PM, wclapham [hidden email] http://n4.nabble.com/user/SendEmail.jtp?type=nodenode=1695531i=0 http://n4.nabble.com/user/SendEmail.jtp?type=nodeamp;node=1695531amp;i=0 wrote: I have a data frame that I created using read.table on a csv spreadsheet. The data look like the following: Steer.ID stocker.trt Finish.trt Date Days Wt .. Steer.Id, stocker.trt, Finish.trt are factors-- Date, Days, Wt are data that are repeated 23 times (wide format). I want to reshape the data such that I have the correct Steer.ID, stocker.trt, Finish.trt identifying all of the repeated measures data in a long format. I am a newbie at R and need to develop the skill in reshaping data, so that I can handle routine problems like described above. Thanks so much in advance for help or advice. Bill -- View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-r epeated-measures-tp1695500p1695500.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] http://n4.nabble.com/user/SendEmail.jtp?type=nodenode=1695531i=1 http://n4.nabble.com/user/SendEmail.jtp?type=nodeamp;node=1695531amp;i=1 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. -- View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-repeated-measures-tp1695500p1745223.html Sent from the R help mailing list archive at Nabble.com. [[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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ 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] Reshaping a data frame with a series of factors and 23 repeated measures
I have a data frame that I created using read.table on a csv spreadsheet. The data look like the following: Steer.ID stocker.trt Finish.trt Date Days Wt .. Steer.Id, stocker.trt, Finish.trt are factors-- Date, Days, Wt are data that are repeated 23 times (wide format). I want to reshape the data such that I have the correct Steer.ID, stocker.trt, Finish.trt identifying all of the repeated measures data in a long format. I am a newbie at R and need to develop the skill in reshaping data, so that I can handle routine problems like described above. Thanks so much in advance for help or advice. Bill -- View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-repeated-measures-tp1695500p1695500.html Sent from the R help mailing list archive at Nabble.com. __ 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] Reshaping a data frame with a series of factors and 23 repeated measures
Hi Bill, Without an example dataset it's hard to see exactly what you need to do. But you can get started by looking at the documentation for the reshape function (?reshape), and by looking at the reshape package. The reshape package has an associated web page (http://had.co.nz/reshape/) with links to papers and other information to help you get started. Best, Ista On Mon, Mar 29, 2010 at 3:15 PM, wclapham william.clap...@ars.usda.gov wrote: I have a data frame that I created using read.table on a csv spreadsheet. The data look like the following: Steer.ID stocker.trt Finish.trt Date Days Wt .. Steer.Id, stocker.trt, Finish.trt are factors-- Date, Days, Wt are data that are repeated 23 times (wide format). I want to reshape the data such that I have the correct Steer.ID, stocker.trt, Finish.trt identifying all of the repeated measures data in a long format. I am a newbie at R and need to develop the skill in reshaping data, so that I can handle routine problems like described above. Thanks so much in advance for help or advice. Bill -- View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-repeated-measures-tp1695500p1695500.html Sent from the R help mailing list archive at Nabble.com. __ 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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ 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] reshaping a data frame
Dear R People: Suppose I have a data.frame, x.df, which has one column and 100 rows. Sometimes I need to see it as 20 rows and 5 columns, sometimes I need to see it as 10 rows with 10 columns. Is there a quick way to make those transformations, please? Thanks in advance, Sincerely, Erin PS Happy Labour Day in New Zealand! -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodg...@gmail.com __ 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] reshaping a data frame
Why not just unlist the data.frame and specify the number of rows in a matrix each time you need look at a different layout, assuming I understand the question. Example === aa - data.frame(1:100) bb - unlist(aa) (cc - matrix(bb, nrow=20)) --- On Mon, 10/26/09, Erin Hodgess erinm.hodg...@gmail.com wrote: From: Erin Hodgess erinm.hodg...@gmail.com Subject: [R] reshaping a data frame To: R help r-h...@stat.math.ethz.ch Received: Monday, October 26, 2009, 4:09 PM Dear R People: Suppose I have a data.frame, x.df, which has one column and 100 rows. Sometimes I need to see it as 20 rows and 5 columns, sometimes I need to see it as 10 rows with 10 columns. Is there a quick way to make those transformations, please? Thanks in advance, Sincerely, Erin [[elided Yahoo spam]] -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodg...@gmail.com __ 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. __ [[elided Yahoo spam]] __ 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] reshaping a data frame
On 10/27/2009 07:09 AM, Erin Hodgess wrote: Dear R People: Suppose I have a data.frame, x.df, which has one column and 100 rows. Sometimes I need to see it as 20 rows and 5 columns, sometimes I need to see it as 10 rows with 10 columns. Is there a quick way to make those transformations, please? matrix(unlist(x),nrow=20) and maybe byrow=TRUE depending upon how you want the values distributed. Jim __ 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.