Skip to content
Prev 336780 / 398513 Next

Aggregation

Hi,
May be this helps:
DataA <- read.table(text="ID,Var1,Var2
1,A,100
1,B,50
2,A,200
2,B,100
2,B,50",sep=",",header=TRUE,stringsAsFactors=FALSE)

dcast(DataA,ID~Var1,value.var="Var2")? ## I guess this is what you mentioned
#Aggregation function missing: defaulting to length
#? ID A B
#1? 1 1 1
#2? 2 1 2
?DataB <- ddply(DataA,.(ID,Var1),transform,Var1ID=paste0(ID,seq_along(Var1)))
?dcast(DataB,ID+Var1ID~Var1,value.var="Var2")[,-2]
# ID?? A?? B
#1? 1 100? 50
#2? 2 200 100
#3? 2? NA? 50

A.K.
On Tuesday, February 18, 2014 6:08 PM, farnoosh sheikhi <farnoosh_81 at yahoo.com> wrote:
For some reason, I get frequency not the values although I have var2 as numeric and no missing values.
Why is that?

Regards,?Farnoosh Sheikhi
On Thursday, February 13, 2014 1:29 PM, arun <smartpink111 at yahoo.com> wrote:
Sorry, the library should be 
library(reshape2)
On Thursday, February 13, 2014 4:27 PM, arun <smartpink111 at yahoo.com> wrote:
HI Farnoosh,

You can use ?dcast()
library(plyr)

?dcast(DataA,ID~Var1,value.var="Var2")
#? ID?? A?? B
#1? 1 100? 50
#2? 2 200 100
A.K.
On Thursday, February 13, 2014 2:59 PM, farnoosh sheikhi <farnoosh_81 at yahoo.com> wrote:
Hi Arun,

I hope all is well. I need to aggregate a data like below:

DataA

ID ? ? ? Var1 ? Var2
1? ? ? ? A100
1B50
2? ? ? ? A200
2B100


The transformed data should
be like below:
ID ? AB
1100 ? 50
2200100


Thanks a lot.

Regards,?Farnoosh Sheikhi
Message-ID: <1392775266.42284.YahooMailNeo@web142604.mail.bf1.yahoo.com>
In-Reply-To: <1392764901.37103.YahooMailNeo@web141703.mail.bf1.yahoo.com>