[R] Creating a variable which is the sum of equal rows in a dataframe

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 4 13:56:50 CEST 2009


Try aggregate. First we read the data into DF and
then apply aggregate:

> Lines <- "Firm                     Banks
+ 500600700          Citybank
+ 500600700          CGD
+ 500600700          BES
+ 500600800          Citybank
+ 500600800          Bank1
+ 500600900          CGD"
> # DF <- read.table("myfile.dat", header = TRUE)
> DF <- read.table(textConnection(Lines), header = TRUE)
>
> aggregate(DF["Banks"], DF["Firm"], length)
       Firm Banks
1 500600700     3
2 500600800     2
3 500600900     1

On Mon, May 4, 2009 at 7:19 AM, Cecilia Carmo <cecilia.carmo at ua.pt> wrote:
> Hi everyone:
>
> I need to count the number of banks of each firm in my data. The firm is
> identified by the fiscal number. The banks of each firm appears like this:
>
> Firm                     Banks
> 500600700          Citybank
> 500600700          CGD
> 500600700          BES
> 500600800          Citybank
> 500600800          Bank1
> 500600900          CGD
>>
> I want to obtain the following dataframe:
> Firm            numberofbanks
> 500600700          3
> 500600800          2
> 500600900          1
>>
> This is a question of counting the times each firm appears, but I don’t know
> which function do this. If anyone could help me I appreciate.
>
> Thank you in advance for the help you could give me,
>
> Cecilia Carmo (Portugal)
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list