0 votes
1 view
in R Programming by (5.2k points)

I'm trying to transfer my understanding of plyr into dplyr, but I can't figure out how to group by multiple columns.

# make data with weird column names that can't be hard coded

data = data.frame(

  asihckhdoydkhxiydfgfTgdsx = sample(LETTERS[1:3], 100, replace=TRUE),

  a30mvxigxkghc5cdsvxvyv0ja = sample(LETTERS[1:3], 100, replace=TRUE),

  value = rnorm(100)

)

# get the columns we want to average within

columns = names(data)[-3]

# plyr - works

ddply(data, columns, summarize, value=mean(value))

# dplyr - raises error

data %.%

  group_by(columns) %.%

  summarise(Value = mean(value))

#> Error in eval(expr, envir, enclos) : index out of bounds

What am I missing to translate the plyr example into a dplyr-esque syntax?

1 Answer

0 votes
by (25.3k points)

You can use the group_by_at function from the dplyr package to group by multiple columns using string vector inputs.

The basic syntax is as follows:

group_by_at(.tbl, .vars, .funs = list(), ..., .add = FALSE,

  .drop = group_by_drop_default(.tbl))

Where

.tbl

A tbl object.

.funs

A function fun, a quosure style lambda ~ fun(.) or a list of either form.

...

Additional arguments for the function calls in .funs. These are evaluated only once, with tidy dots support.

.drop

When .drop = TRUE, empty groups are dropped. 

.vars

A list of columns generated by vars(), a character vector of column names, a numeric vector of column positions, or NULL.

one_of(): Variables in character vector

In your case:

data = data.frame(

  asihckhdoydkhxiydfgfTgdsx = sample(LETTERS[1:3], 100, replace=TRUE),

  a30mvxigxkghc5cdsvxvyv0ja = sample(LETTERS[1:3], 100, replace=TRUE),

  value = rnorm(100)

)

columns = names(data)[-3]

data %>%

  group_by_at(vars(one_of(columns))) %>%

  summarize(MeanValue = mean(value))

Output:

# A tibble: 9 x 3

# Groups:   asihckhdoydkhxiydfgfTgdsx [3]

  asihckhdoydkhxiydfgfTgdsx a30mvxigxkghc5cdsvxvyv0ja MeanValue

  <fct>                     <fct>                         <dbl>

1 A                         A                             0.588

2 A                         B                             0.113

3 A                         C                             0.178

4 B                         A                            -0.397

5 B                         B                            -0.118

6 B                         C                            -0.461

7 C                         A                             0.117

8 C                         B                            -0.583

9 C                         C                            -0.382

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...