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

Fredrik Karlsson dargosch at gmail.com
Mon May 4 13:41:37 CEST 2009


Hi Cecilia,

You can use table for this:

#Generate a dataframe
> dat.df <- data.frame(Firm = sample(c(500600700,500600800,500600800),6,replace=TRUE),Banks=sample(c("Citybank","CGD","DES","Bank1"),6,replace=TRUE)  )

#Get the counts in a table format
> with(dat.df, table(Firm,Banks))
           Banks
Firm        Bank1 CGD Citybank DES
  500600700     0   0        1   0
  500600800     2   1        1   1

#Or, if you prefer the data.frame format, you can reshape it.
> as.data.frame(with(dat.df, table(Firm,Banks)))
       Firm    Banks Freq
1 500600700    Bank1    0
2 500600800    Bank1    2
3 500600700      CGD    0
4 500600800      CGD    1
5 500600700 Citybank    1
6 500600800 Citybank    1
7 500600700      DES    0
8 500600800      DES    1

Hope this helps.

/Fredrik

On Mon, May 4, 2009 at 1:19 PM, 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.
>



-- 
"Life is like a trumpet - if you don't put anything into it, you don't
get anything out of it."




More information about the R-help mailing list