Home > Net >  how to pass column names including space in R
how to pass column names including space in R

Time:04-08

assume my column names are: User ID and name
how should I pass this column name to functions like what I have below?

df %>%
  group_by(User ID) %>%
  count(name)

apparently, group_by() or similar functions do not accept column names with space in their names.

CodePudding user response:

You need to use tibble instead of data.frame:

library(tidyverse)

df <- tibble(`User ID` = 1:2, x = 5:6)
  
df %>%
  group_by(`User ID`) %>% 
  summarise(total = sum(x))

#> # A tibble: 2 × 2
#>   `User ID` total
#>       <int> <int>
#> 1         1     5
#> 2         2     6
  • Related