2

I want to find the average of given sets of numbers of varying length in R.

The data I have looks like this:

    company_name  marketcap  date
    A             100023     01-01-2000
    A             100234     02-01-2000
    A             108332     03-01-2000
    A
    .
    .
    A             112334     31-12-2000
    B              24342     01-01-2000
    B              25345     02-01-2000
.
.

There are hundred such companies. I want to calculate average value of marketcap for each company.

Output should look something like this (each marketcap value is an average rather than a set of numbers)

    company_name   marketcap  
    A              1023452   
    B               243425
.
.

2 Answers2

2

We can use aggregate from base R to get the mean of 'marketcap' grouped by 'company_name'

aggregate(marketcap ~ company_name, df1, FUN = mean)
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Or you can use the aggregation/group-by of the data.table library as:

library(data.table)

dt <- as.data.table(your_data_frame)
dt[, .(marketcap = mean(marketcap)), by = company_name)]
Newl
  • 310
  • 2
  • 12