-1

Error : Error in grouped_df_impl(data, unname(vars), drop) : Column col1 is unknown

pivot <- function(df, col1, col2){
  df %>%
    group_by(col1, col2) %>%
    summarise(n = n()) %>%
    spread(col2, n, fill = 0)
}

Running the command pivot (iris, Species, Petal.Width)

Can anyone tell why this error message ? and how to solve it

2 Answers2

2

You need to use group_by_ for programming.

library(tidyverse)
pivot <- function(df, col1, col2){
  df %>%
    group_by_(col1, col2) %>%
    summarise(n = n()) %>%
    spread(col2, n, fill = 0)
}

pivot (iris, "Species", "Petal.Width")

This should work too. Don't need to quote the columns here.

pivot <- function(df, col1, col2) {
  group_var1 <- enquo(col1)
  group_var2 <- enquo(col2)
  df %>% 
    group_by(!!group_var1, !!group_var2) %>% 
    summarise(n = n())
}

pivot (iris, Species, Petal.Width)

The output on the second is easier to read, I think.

For more information, see dplyr: How to use group_by inside a function? and https://dplyr.tidyverse.org/articles/programming.html.

hmhensen
  • 2,974
  • 3
  • 22
  • 43
1

Another answer is:

pivot <- function(df, col1, col2){
  col1<-deparse(substitute(col1))
  col2<-deparse(substitute(col2))
  df %>%
    group_by_(col1, col2) %>%
    summarise(n = n()) %>%
    spread(col2, n, fill = 0)
}

pivot (iris, Species, Petal.Width)
NelsonGon
  • 13,015
  • 7
  • 27
  • 57