Skip to content
Back to formatted view

Raw Message

Message-ID: <20160621094937.Horde.Sz1McbL3wnVXaP_hZLgin2A@mail.sapo.pt>
Date: 2016-06-21T08:49:37Z
From: Rui Barradas
Subject: Data aggregation
In-Reply-To: <CAOgs+Oy3RGqi9yFbaO=Be-ZU2=1jMCa=3sYQPWk37PwYnBFvHw@mail.gmail.com>

Hello,

Try the following.

dat <- read.csv(text = "
Regime, Industry, Cost
10, 01, 370
11, 01, 400
10, 02, 200
10, 01, 500
11, 02, 60
10, 02, 30
")

dat

res <- aggregate(Cost ~ Industry + Regime, data = dat, sum)

res <- res[order(res$Industry), ]
res

And see the help page ?aggregate

Hope this helps,

Rui Barradas


Quoting Paolo Letizia <paolo.letizia at gmail.com>:

> Dear All:
> I have a data frame with 3 columns: "Regime", "Industry", and "Cost".
> I want to sum the value of "Cost" for each industry and "Regime".
> Example:
>
> The data frame is:
> Regime, Industry, Cost
> 10, 01, 370
> 11, 01, 400
> 10, 02, 200
> 10, 01, 500
> 11, 02, 60
> 10, 02, 30
>
> I want the following output:
> 01, 10, 870
> 01, 11, 400
> 02, 10, 230
> 02, 11, 600
>
> Can you please help me on this? Paolo
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.