gogs09
gogs09

Reputation: 169

How to filter data based on a column name dynamically in R?

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?

Upvotes: 2

Views: 653

Answers (2)

akrun
akrun

Reputation: 887311

We can use tidyverse

library(dplyr)
df1 %>% 
      group_by_(x_axis_column) %>% 
      summarise(Frequency = sum(get(item)))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389055

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)

Upvotes: 2

Related Questions