Dplyr 0.5.0 mutate using column index

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

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 am getting the following error

Error: `.vars` must be a character/numeric vector or a `vars()` object, 
not list

      

+3


source to share


1 answer


New prototype mutate_at

:

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

      



Note what .vars

is the first argument. This way you either explicitly specify .vars

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

      

+4


source







All Articles