在mutate_if调用中提取列名称 [英] Extract column name in mutate_if call

查看:73
本文介绍了在mutate_if调用中提取列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在对mutate_if的函数调用中提取列名.有了这个,我便想在 不同的表格,并使用查找值填写缺失值.我尝试使用quosure语法,但无法正常工作. 是否可以直接提取列名?

I would like to extract the column name in the function call to mutate_if. With this, I then want to look up a value in a different table and fill in missing values with the lookup value. I tried using quosure syntax, but it is not working. Is there a possibility to extract the column name directly?

样本数据

df <- structure(list(x = 1:10, 
               y = c(1L, 2L, 3L, NA, 1L, 2L, 3L, NA, 1L, 2L), 
               z = c(NA, 2L, 3L, NA, NA, 2L, 3L, NA, NA, 2L), 
               a = c("a", "b", "c", "d", "e", "a", "b", "c", "d", "e")), 
          .Names = c("x", "y", "z", "a"), 
          row.names = c(NA, -10L), 
          class = c("tbl_df", "tbl", "data.frame"))
df_lookup <- tibble(x = 0L, y = 5L, z = 8L)

不起作用

直接以某种方式提取名称无效.

It does not work to extract the name somehow directly.

df %>% 
  mutate_if(is.numeric, funs({
    x <- .
    x <- enquo(x)
    lookup_value <- df_lookup %>% pull(quo_name(x))
    x <- ifelse(is.na(x), lookup_value, x)
    return(x)
  }))

使用附加功能,我可以提取名称,但是替换不再起作用.

With an extra function I'm able to extract the name but then the replacement doesn't work anymore.

custom_mutate <- function(v) {
  v <- enquo(v)
  lookup_value <- df_lookup %>% pull(quo_name(v))

  # ifelse(is.na((!!v)), lookup_value, (!!v))
}

df %>% 
  mutate_if(is.numeric, funs(custom_mutate(v = .)))

工程

如果将df作为附加参数添加到自定义函数中,它将起作用,但是有没有这种方法吗?感觉不对,而不是dplyr的含义...如果我错了,请纠正我;)
除此之外,我必须使用UQE而不是!!,正如它在编程中所说的那样与dplyr :

If I add the df as an additional argument to my custom function it works, but is there a way without this? It feels wrong and not how dplyr is meant to be... Correct me if I'm wrong ;)
In addition to this I have to use UQE instead of !! and as it says in Programming with dplyr:

UQE()仅供专家使用

UQE() is for expert use only

custom_mutate2 <- function(v, df) {
  v <- enquo(v)
  lookup_value <- df_lookup %>% pull(quo_name(v))

  df %>% 
    mutate(UQE(v) := ifelse(is.na((!!v)), lookup_value, (!!v))) %>% 
    pull(!!v)
}

df %>% 
  mutate_if(is.numeric, funs(custom_mutate2(v = ., df = df)))

预期输出

# A tibble: 10 x 4
#        x     y     z a    
#    <int> <int> <int> <chr>
#  1     1     1     8 a    
#  2     2     2     2 b    
#  3     3     3     3 c    
#  4     4     5     8 d    
#  5     5     1     8 e    
#  6     6     2     2 a    
#  7     7     3     3 b    
#  8     8     5     8 c    
#  9     9     1     8 d    
# 10    10     2     2 e   

推荐答案

您必须使用quo而不是enquo

#enquo(.) :
<quosure: empty>
~function (expr) 
{
    enexpr(expr)
}
...

#quo(.) :
<quosure: frame>
~x
<quosure: frame>
~y
<quosure: frame>
~z

以您的示例为例:

mutate_if(df, is.numeric, funs({
  lookup_value <- df_lookup %>% pull(quo_name(quo(.)))
  ifelse(is.na(.), lookup_value, .)
}))

# A tibble: 10 x 4
       x     y     z a    
   <int> <int> <int> <chr>
 1     1     1     8 a    
 2     2     2     2 b    
 3     3     3     3 c    
 4     4     5     8 d    
 5     5     1     8 e    
 6     6     2     2 a    
 7     7     3     3 b    
 8     8     5     8 c    
 9     9     1     8 d    
10    10     2     2 e    

这篇关于在mutate_if调用中提取列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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