vpap
vpap

Reputation: 1557

R, pass grouped column as string to summarize function argument

In the code below,

# data frame df has columns A, B, C, D, E and few more
tmp = group_by( df, A, B, C, D)
tmp = summarise( tmp, NewColumn = min( E))

I would like to change column E in the second row to a sting or any format that will allow to have these two lines of code in a function i.e. (one possible format)

getSummary = function( tmp = NULL, col2pass = 'E') {

                       tmp = group_by( df, A, B, C, D)
                       tmp = summarise( tmp, NewColumn = min( col2pass))

                       return( tmp)
             }

I tried tmp[ , colnames( tmp)[[ index_of_E]]], tmp[ 'E'] or just "E" but it doesn't work apparently; in the first two cases the grouping is lost and in the second case the string is interpreted as a literal string.

Thanks

Upvotes: 1

Views: 485

Answers (2)

akrun
akrun

Reputation: 887691

We can use summarise_at

library(dplyr)
tmp %>%
     summarise_at(vars(col2pass), min) 

Upvotes: 0

linog
linog

Reputation: 6226

You face the tricky problem of standard evaluation with dplyr. If you start to write functions using variable names, you might find data.table easier to handle (you have examples in a post I made)

dplyr

With dplyr you can use rlang to unquote variables:

tmp = summarise( tmp, NewColumn = min(!!rlang::sym(col2pass)))

You can also use {{}} syntax to unquote (recent feature):

tmp = summarise(tmp, NewColumn = min({{col2pass}}))

data.table

You can write:

library(data.table)
setDT(tmp)
tmp[, .('NewColumn' = get(col2pass))]

Upvotes: 2

Related Questions