dplyr do()没有点代词? [英] dplyr do() without dot pronoun?

查看:173
本文介绍了dplyr do()没有点代词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dplyr :: do()中,是否有一种在 .data 的上下文中访问变量的方法和其他dplyr函数一样?例如,说我有一个数据框:

 > dd<  -  data.frame(a = 1:5)

mutate ()以及其他功能,以便表达式在数据框架的上下文中进行评估:

 >变异(dd,a2 = a * 2)
a a2
1 1 2
2 2 4
3 3 6
4 4 8
5 5 10

但不是 do

 > do(dd,data.frame(a2 = a [1:2] * 2))
eval(expr,envir,enclosure)中的错误:对象'a'未找到

我可以使用与()和点代词

 > do(dd,with(。,data.frame(a2 = a [1:2] * 2)))
a2
1 2
2 4

我也不知道为什么这不起作用:

 > do(dd,function(X)data.frame(a2 = X $ a [1:2] * 2))
错误:结果必须是数据框
/ pre>

问题:


  1. 有什么逻辑为什么这个行为(范围)与
    mutate 选择等不同

  2. 有没有一个优雅的解决方案,或者我有
    继续使用 with()如果我不想继续使用
    。$ variablename 在表达式中?

  3. 为什么匿名
    函数不起作用?似乎这样工作这里,但不确定为什么我的
    的情况是不同的。


解决方案


    <

    mutate select 等是专门设计的数据参数的功能, code> magrittr 并帮助非标准评估。 do 是一个更通用的功能,无法做出相同的假设。


  1. 这取决于你正在努力做如果您使用非标准评估功能,您只需要提供。一次。


例如:

  do(dd,transform(。,a2 = a * 2)[1:2,] [a2])
a2
1 2
2 4

但是,真的没有比更好的最好的代码高尔夫是:

  do(dd,data.frame(a2 =。$ a [1:2] * 2))
a2
1 2
2 4

但是这取决于您想要参考原始数据框架的频率。您可能会发现使用带有管道功能的特殊功能更容易,更可读。


  1. 您需要调用匿名功能,否则它只是返回函数定义。

像这样:

  do(dd,{function(X)data.frame(a2 = X $ a [1:2] * 2)}(。))
a2
1 2
2 4


In dplyr::do(), is there a way to access variables in the context of .data as with other dplyr functions? For instance, say I have a data frame:

> dd <- data.frame(a=1:5)

mutate(), as well as other functions, works so that expressions are evaluated in the context of the data frame:

> mutate(dd,a2=a*2)
  a a2
1 1  2
2 2  4
3 3  6
4 4  8
5 5 10

But not do:

> do(dd,data.frame(a2=a[1:2]*2))
Error in eval(expr, envir, enclos) : object 'a' not found

I can accomplish my objective using with() and the dot pronoun:

> do(dd,with(.,data.frame(a2=a[1:2]*2)))
  a2
1  2
2  4

I am also not sure why this doesn't work:

> do(dd,function(X) data.frame(a2=X$a[1:2]*2))
Error: Result must be a data frame

Questions:

  1. Is there logic to why this behavior (scope) is different from mutate, select, etc.
  2. Is there an elegant solution or I have to keep using with() if I don't want to keep using .$variablename in the expression?
  3. Why doesn't the anonymous function work? Seems like it works here but not sure why my case is different.

解决方案

  1. mutate, select, etc are specialised functions designed to have the data argument first which work with piping of magrittr and help with non-standard evaluation. do is a more general function which can not make the same assumptions.

  2. It depends on what you are trying to do. If you use a function with non-standard evaluation, you will only need to provide the . once.

For example:

do(dd, transform(.,a2=a*2)[1:2,]["a2"])
  a2
1  2
2  4

But it is no better than with really. The best code golf would be:

do(dd, data.frame(a2=.$a[1:2]*2))
  a2
1  2
2  4

But it depends on how often you want to refer to the original data.frame. You may find it easier and more readable to use the specialsed functions with piping for this task.

  1. You need to call the anonymous function otherwise it is just returning the function definition.

Like so:

do(dd,{function(X) data.frame(a2=X$a[1:2]*2)}(.))
  a2
1  2
2  4

这篇关于dplyr do()没有点代词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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