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

baptiste auguie ba208 at exeter.ac.uk
Mon May 4 13:34:12 CEST 2009


Try this,

# d <- read.table(pipe("pbpaste"), head=T) # read your data

table(d)

# library(reshape)
cast(as.data.frame(table(d)), .~Firm, fun=sum)


HTH,

baptiste


On 4 May 2009, at 13:19, Cecilia Carmo 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list