2

I am performing a routine in R, where I need to sum a variable by group. I saw this post and used aggregate. It works fine when I hardcode the column names such as Category and Frequency.

Category     Frequency
First        10
First        15
First        5
Second       2
Third        14
Third        20
Second       3

But, my situation is that, these column names must be assigned dynamically in run-time. These columns are created during run-time based on some manipulation. So, if I try to access the column name with a variable like this -

x_axis_column <- input$x_axis

and try to use the aggregate function, I get an error as shown below -

Error in FUN: invalid 'type' (character) of argument

This is the code that I tried -

counted <- aggregate(item~x_axis_column, data, sum)

What am I doing wrong here?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
gogs09
  • 169
  • 2
  • 16

2 Answers2

2

As the column names are stored in a variable, we cannot directly utilise them. One way is to use the variables to subset the dataframe.

aggregate(df[item], df[x_axis_column], sum)

#  Category Frequency
#1    First        30
#2   Second         5
#3    Third        34

Or another option is with using formula and get

aggregate(get(item)~get(x_axis_column), df, sum)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks it worked. But I get the column names of the new data-frame as `get(item)` and `get(x_axis_column)`. Why is that so? – gogs09 May 31 '17 at 05:51
  • 1
    @DLML you can use another option provided, or use `setnames` with `get`, as shown [here](https://stackoverflow.com/questions/15309205/name-columns-within-aggregate-in-r). – Ronak Shah May 31 '17 at 05:56
  • Thanks a lot! I get it now. – gogs09 May 31 '17 at 05:58
0

We can use tidyverse

library(dplyr)
df1 %>% 
      group_by_(x_axis_column) %>% 
      summarise(Frequency = sum(get(item)))
akrun
  • 874,273
  • 37
  • 540
  • 662