-2

My code need to group by column names. The problem that the code adds or removes columns to data.frame automatically, thus putting columns names by hand is not good solution. Is there work around this problem. I tried solutions like this but obviously it doesn’t work. In addition the dataframe stretches to over 100 columns.

myDataFrame1   <- myDataFrame  %>% group_by( colnames(myDataFrame) )  

How can I paste the column names into group_by() automatically.

Thanks for help

ekad
  • 14,436
  • 26
  • 44
  • 46
Jad Gift
  • 305
  • 4
  • 15

1 Answers1

-3

We can make use of the group_by_ if there are more columns. Suppose, we want to have the first three columns as the grouping variable,

library(dplyr)
myDataFrame  %>%
       group_by_(.dots = names(myDataFrame)[1:3])
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    thank you so much, It works like a charm. I just upgraded your code and works perfecto `[1:3]` raplaced with `[1, ncol(myDataFrame)]` : `myDataFrame %>% group_by_(.dots = names(myDataFrame)[1:ncol(myDataFrame)])` – Jad Gift May 12 '17 at 07:30