Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a variable name in a formula instead of the column itself

I have data for which I would like to make a summary by group using the summary_by function (from the doBy package). I can’t use the column names in the summary_by formula but variables I created before.
Below is the result I would like to achieve :

library(data.table)
library(doBy)

mtcars = data.table(mtcars)

doBy::summary_by(data = mtcars, mpg ~ gear + am, FUN = "mean")

output:

gear  am   mpg."mean"
3     0    16.10667
4     0    21.05000
4     1    26.27500
5     1    21.38000

Here is what I want to do :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(data.table)
library(doBy)

mtcars = data.table(mtcars)

variable1 = "gear" # which is a column name of mtcars
variable2 = "am" # which is a column name of mtcars
variable3 = "mpg" # which is a column name of mtcars

doBy::summary_by(data = mtcars, variable3 ~ variable1 + variable2 , FUN = "mean")

I tried with the functions get, assign, eval, mget but I don’t find the solution.

>Solution :

Just provide a string instead of a formula that relies on non-standard evaluation.

library(data.table)
library(doBy)

mtcars = data.table(mtcars)

variable1 = "gear" # which is a column name of mtcars
variable2 = "am" # which is a column name of mtcars
variable3 = "mpg" # which is a column name of mtcars

doBy::summary_by(data = mtcars, 
                 # alternatively to sprintf(), use paste() oder glue()
                 as.formula(sprintf("%s ~ %s + %s", variable3, variable1, variable2)), 
                 FUN = "mean")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading