mutate()在使用美元符号运算符时尝试使用全局变量的值来提取 [英] mutate() is trying to extract using the value of a global variable when using the dollar sign operator

查看:165
本文介绍了mutate()在使用美元符号运算符时尝试使用全局变量的值来提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 mutate $ 中提取有趣的结果,当在与要提取的元素名称相同的全局环境。 (我正在运行R 3.1.3和dplyr 0.4.3.9。)这工作正常:

  library(dplyr)

df< - data.frame(time = 1:5,val = c(2.3,3.9,NA,8.1,9.6))

mutate(df,val =约(time,val,time)$ y)
#time val
#1 1 2.3
#2 2 3.9
#3 3 6.0
#4 4 8.1
#5 5 9.6

但是,如果我定义一个全局变量 y ,有趣的事情发生:

  y<  -  1L 
mutate(df,val =约(时间,值,时间)$ y)
#错误:无效下标类型'整数'

请注意,使用带有字符串参数的双括号仍然按预期工作:

  mutate(df,val =时间,时间,时间)[b] 
#time val
#1 1 2.3
#2 2 3.9
#3 3 6.0
# 4 4 8.1
#5 5 9.6

有趣的是,我们得到一个不同类型的错误,如果 y 是一个字符:

  y<  - 'a'
mutate(df,val = approx(time,val,time) $ y)
#错误:列'val'不支持的类型(NILSXP,classes = NULL)



最后,为了完整起见,下面是一个例子,证明这绝对不是列表提取的通常行为:

  l < -  list(y = 1:4)
y< - 'a'
l $ y
#[1] 1 2 3 4
有没有人知道为什么我们在 mutate 之间得到这个奇怪的行为?有没有一个简单的方法来解决这个问题,除了使用双括号提取或确保搜索路径上没有冲突的变量?



顺便说一下,它看起来像下面的帖子中的OP可能有同样的问题,但并没有意识到:
dplyr mutate failed with named vector?

解决方案

正如@clbieganek指出的,是一个bug它尚未修复(从dplyr版本4.3)



可能的修复建议在评论:



$'y'



这是跟踪这个一般问题的问题:
https://github.com/hadley/dplyr/issues/1400


I'm getting funny results using mutate with a $ extraction when there happens to be a variable in the global environment with the same name as the element being extracted. (I'm running R 3.1.3 and dplyr 0.4.3.9.) This works fine:

library(dplyr)

df <- data.frame(time = 1:5, val = c(2.3, 3.9, NA, 8.1, 9.6))

mutate(df, val = approx(time, val, time)$y)
#   time val
# 1    1 2.3
# 2    2 3.9
# 3    3 6.0
# 4    4 8.1
# 5    5 9.6

But if I define a global variable y, funny things happen:

y <- 1L
mutate(df, val = approx(time, val, time)$y)
# Error: invalid subscript type 'integer'

Note that using double brackets with a string argument still works as expected:

mutate(df, val = approx(time, val, time)[['y']])
#   time val
# 1    1 2.3
# 2    2 3.9
# 3    3 6.0
# 4    4 8.1
# 5    5 9.6

Interestingly, we get a different type of error if y is a character:

y <- 'a'
mutate(df, val = approx(time, val, time)$y)
# Error: unsupported type for column 'val' (NILSXP, classes = NULL)

Finally, for completeness, here's an example that demonstrates that this is definitely not the usual behavior for list extraction:

l <- list(y = 1:4)
y <- 'a'
l$y
# [1] 1 2 3 4

Does anyone know why we get this weird behavior inside mutate? And is there an easy way to fix this problem, aside from using double brackets for extraction or ensuring that there are no conflicting variables on the search path?

By the way, it looks like the OP in the following post might have had the same problem but didn't quite realize it: dplyr mutate fails with named vector?

解决方案

As @clbieganek pointed out, this is a bug. It is not yet fixed (as of dplyr version 4.3)

Possible fix as suggested in comments:

$'y'

This is the issue that tracks this general problem: https://github.com/hadley/dplyr/issues/1400

这篇关于mutate()在使用美元符号运算符时尝试使用全局变量的值来提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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