0

I have a scenario where I get the column index in a variable and the I have to group by and summarise by that variable

 col_index <- which(sapply(dataframe, function(x) any(x == "Area of Maintenance")))

> col_index
  X__7 
  8 

Now I want to group by col_index value like following

df%>%
group_by(df[col_index]) %>%
summarise(count = n()) %>%
as.data.frame()

It gives me following error.

Error in mutate_impl(.data, dots) : 
Evaluation error: Column index must be at most 1 if positive, not 8.

col_index has a dynamic value. How can I do it in r?

Neil
  • 7,937
  • 22
  • 87
  • 145

2 Answers2

2

You can group by all columns that match the given function using group_by_if:

df %>%
    group_by_if(function(x) any(x == "Area of Maintenance")) %>%
    summarise(count = n()) %>%
    as.data.frame()
Marius
  • 58,213
  • 16
  • 107
  • 105
  • Thanks for your answer,but what if I want to do so with `col_index` ? Can we do it this way? – Neil Sep 27 '17 at 05:06
2

Try the following:

col_index <- which(sapply(colnames(dataframe), function(x) any(x == "Area of Maintenance")))

df%>%
group_by(.[[col_index]]) %>%
summarise(count = n()) %>%
as.data.frame()

Note: I had to use colnames in sapply to get it to correctly work on my machine

Credit: I took help from dplyr: how to reference columns by column index rather than column name using mutate?

Imran Ali
  • 2,223
  • 2
  • 28
  • 41