0

I am very new to R - I am going through a script that includes the following:

new_data<-aggregate(. ~ old_data$Company+old_data$Financial_Year, data=old_data, sum)

I understand that this is what in pandas would be a groupby().sum() where everything is grouped at the company and financial year level - is that correct? is '.~' meaning all the other columns in old_data other than Company and Financial_Year?

Also if the command were to throw the error

Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument

I imagine it is because some of the other columns are not numeric?

thanks in advance!

Filippo Sebastio
  • 1,112
  • 1
  • 12
  • 23
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. The `~` indicates a formula. When using the formula syntax, you should not use the `$` operator. – MrFlick Mar 22 '22 at 01:16
  • can't share the data but this is the result of str(old_data): $ Financial_Year : chr, Company : chr and everything else is a numeric – Filippo Sebastio Mar 22 '22 at 01:22
  • 1
    I can't reproduce the error. This works fine for me: `old_data <- data.frame(Company=c("a","a","b","b"),Financial_Year=c("17","17","17","17"),One=c(1,2,3,4),Two=c(4,5,6,7)); aggregate(.~Company+Financial_Year, data=old_data, sum)`. Be sure to read the `?aggregate` help page and check the examples for more information about the formula syntax. – MrFlick Mar 22 '22 at 01:27
  • I found out the issue in the script - I had to remove old_data$ from in front Company and Financial - just like you did - thanks for the help!! – Filippo Sebastio Mar 22 '22 at 01:50

0 Answers0