6

I've updated dplyr (now 0.7.1) and a lot of my old code does not work because mutate_each has been deprecated. I use to do something like this (code below) with mutate_each using the column index. I'd do this on hundreds of columns. And I just can't figure out how to use the vars argument correctly with mutate_at. All examples I've seen have been using column names...which I don't want to do. I'm sure it's a simple answer but I've spent too much time trying to figure it out and would appreciate some help very much.

data<-data.frame(numbers=1:10, morenumbers=11:20)
change<-function(x) ifelse(x>10, 1, 2)
newdata<-data%>%mutate_each(funs(change), 1:2)

If I try:

newdata<-data%>%mutate_at(funs(change), vars(1:2))

Or even this:

newdata<-data%>%mutate_at(funs(change), vars(numbers, morenumbers))     

I get the following error

Error: `.vars` must be a character/numeric vector or a `vars()` object, 
not list
Kevin
  • 229
  • 3
  • 9
  • Note that strictly speaking `mutate_all` is the replacement of `mutate_each`. Also there has been a deprecation warning for a while now. – Axeman Jul 13 '17 at 19:59

1 Answers1

12

The new prototype of mutate_at is:

mutate_at(.tbl, .vars, .funs, ..., .cols = NULL)

Notice, that the .vars is the first argument now. So, either you specify .vars explicitly or change the order.

newdata <- data %>% mutate_at(funs(change), .vars = vars(1:2))
# OR
newdata <- data %>% mutate_at(vars(1:2), funs(change))

   numbers morenumbers
1        2           1
2        2           1
3        2           1
4        2           1
5        2           1
6        2           1
7        2           1
8        2           1
9        2           1
10       2           1
Jaap
  • 81,064
  • 34
  • 182
  • 193
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
  • 1
    I new it would be some easy thing I was missing. Thank you so much. I have a deadline and was starting to panic! – Kevin Jul 13 '17 at 19:58