Floo0 Floo0 - 11 months ago 126
R Question

mutate_at for column-names that include a space

Having a data.frame (tibble) as follows:

require(dplyr)
df_org <- df <- data_frame(a=1:2, b=c("A", "B"))
colnames(df) <- paste("X", c("Y", "Z"))

# A tibble: 2 × 2
`X Y` `X Z`
<int> <chr>
1 1 A
2 2 B


Using
mutate_at
throws the following error:

df_org %>% mutate_at(1, sum) # Works as expected
df %>% mutate_at(1, sum) # Error



Error in parse(text = x) : :1:3: unexpected symbol

1: X Y

--------^


Is this a bug? Am i doing anything wrong?

P.S.:
I am using dplyr 0.5.0 on OS X El Capitan 10.11.6

Answer Source

If you use vars for selecting the columns things work as expected. I'm not sure if needing to use vars like this is expected or not.

df %>% mutate_at(vars(1), sum)

# A tibble: 2 x 2
    X Y   X Z
  <int> <chr>
1     3     A
2     3     B