Skip to content
Back to formatted view

Raw Message

Message-ID: <17EE8C62EA18B84B94C2BA96A143064C017219C1@mx01.ispm.unibe.ch>
Date: 2013-03-15T13:27:03Z
From: Blaser Nello
Subject: Data manipulation
In-Reply-To: <DUB104-ds90393F8F9930A3CA0390DF3ED0@phx.gbl>

Is this what you want to do?

D2 <- expand.grid(Class=unique(D$Class), X=unique(D$X))
D2 <- merge(D2, D, all=TRUE)
D2$Count[is.na(D2$Count)] <- 0

W <- aggregate(D2$Count, list(D2$Class, D2$X), "sum")
W

Best, 
Nello


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of IOANNA
Sent: Freitag, 15. M?rz 2013 13:41
To: r-help at r-project.org
Subject: [R] Data manipulation

Hello all, 

 

I would appreciate your thoughts on a seemingly simple problem. I have a database, where each row represent a single record. I want to aggregate this database so I use the aggregate command :

 

D<-read.csv("C:\\Users\\test.csv")

 

attach(D)

 

by1<-factor(Class)

by2<-factor(X)

W<-aggregate(x=Count,by=list(by1,by2),FUN="sum")

 

The results I get following the form:

 

>W

  Group.1 Group.2 x

1       1     0.1 4

2       2     0.1 7

3       3     0.1 1

4       1     0.2 3

5       3     0.2 4

6       3     0.3 4

 

 

However, what I really want is an aggregation which includes the zero values, i.e.:

 

>W

  Group.1 Group.2 x

1       1     0.1 4

2       2     0.1 7

3       3     0.1 1

4       1     0.2 3

        2     0.2 0

5       3     0.2 4

1        0.3 0

2        0.3 0

6       3     0.3 4

 

 

How can I achieve what I want?

 

Best regards, 

Ioanna