使用group_by时mutate_at评估错误 [英] mutate_at evaluation error when using group_by

查看:116
本文介绍了使用group_by时mutate_at评估错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mutate_at()与group_by()一起使用以及将列位置的数值向量作为第一个(.vars)参数插入时,显示评估错误.

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.

  • 使用R 3.4.2和dplyr 0.7.4版本时出现问题
  • 使用R 3.3.2和dplyr 0.5.0
  • 时工作正常
  • 如果.vars是字符向量(列名),效果很好
  • Issue shows up when using R3.4.2 and dplyr0.7.4 version
  • Works fine when using R3.3.2 and dplyr0.5.0
  • Works fine if .vars is character vector (column name)

示例:

# 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)))

推荐答案

我们获得NA值的原因是,我们从which获得的输出为3,但是我们按"Id"分组,所以只有2之后的专栏.

The reason we are getting NA values is that the output we get from which is 3, but we grouped by 'Id' and so there are only 2 columns after that.

dftest %>%
     group_by(Id) %>% 
     mutate_at(which(names(dftest) %in% nacol)-1, funs(ifelse(is.na(.),0,.)))
# A tibble: 6 x 3
# Groups:   Id [6]
#      Id Month   RWA
#  <fctr> <dbl> <dbl>
#1   10_1     2 0.000
#2   10_2     3 0.000
#3   11_1     4 0.000
#4   11_2     6 1.579
#5   11_3     7 0.000
#6   12_1     8 0.379

此处不需要group_by部分,因为我们将其他列中的NA值更改为0

The group_by is part is not needed here as we are changing NA values in other columns to 0

dftest %>%
    mutate_at(which(names(dftest) %in% nacol), funs(ifelse(is.na(.),0,.)))

这可能是一个错误,使用基于职位的方法有时会带来风险.更好的选择是使用names

It could be a bug and using the position based approach is sometimes risky. Better option would be to go with names

dftest %>%
    group_by(Id) %>% 
    mutate_at(intersect(names(.), nacol), funs(replace(., is.na(.), 0)))

注意:在所有这些情况下,都不需要group_by

NOTE: In all these cases, the group_by is not needed

另一个选项是tidyr中的replace_na

dftest %>%
    tidyr::replace_na(as.list(setNames(0, nacol)))

这篇关于使用group_by时mutate_at评估错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆