mutate_at() shows an evaluation error when used with group_by() and when imputing a numerical vector for column position as the first (.vars) argument.
- Issue shows up when using
R
3.4.2 anddplyr
0.7.4 version - Works fine when using
R
3.3.2 anddplyr
0.5.0 - Works fine if .vars is character vector (column name)
Example:
# Create example dataframe
Id <- c('10_1', '10_2', '11_1', '11_2', '11_3', '12_1')
Month <- c(2, 3, 4, 6, 7, 8)
RWA <- c(0, 0, 0, 1.579, NA, 0.379)
dftest = data.frame(Id, Month, RWA)
# Define column to fill NAs
nacol = c('RWA')
# Fill NAs with last period
dftest_2 <- dftest %>%
group_by(Id) %>%
mutate_at(which(names(dftest) %in% nacol),
funs(ifelse(is.na(.),0,.)))
Error in mutate_impl(.data, dots) :
Evaluation error: object 'NA' not found.
More sensible example demonstrating issue:
# Create example dataframe
Id <- c('10_1', '10_2', '11_1', '11_3', '11_3', '12_1')
Month <- c(2, 3, 4, 6, 7, 8)
RWA <- c(0, 0, 0, 1.579, NA, 0.379)
dftest = data.frame(Id, Month, RWA)
# Define column to fill NAs
nacol = c('RWA')
# Fill NAs with last period
dftest_2 <- dftest %>%
group_by(Id) %>%
mutate_at(which(names(dftest) %in% nacol),
funs(na.locf(., na.rm=F)))