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_dfnames 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
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.vit@gmail.com>wrote:> 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. >[[alternative HTML version deleted]]
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.vit at gmail.com > Sent: Sun, 9 Jun 2013 23:15:48 -0700 > To: r-help at 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 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 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.____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
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.vit at gmail.com> To: "r-help at r-project.org" <r-help at 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 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.
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 <smartpink111 at 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.vit at gmail.com> >> To: "r-help at r-project.org" <r-help at 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 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. >>