为什么我有时必须在`do`中的`.``中的`.` [英] Why do I sometimes have to enclose `.` in `data.frame()` for a named argument in `do`?
问题描述
为什么这不起作用?
data.frame(x = rnorm(100))%>%do(df = )
错误信息:
do _(。data,.dots = lazyeval :: lazy_dots(...))中的错误:
参数.data丢失,没有默认
相反,我必须将。
在 data.frame()
:
data.frame(x = rnorm(100))%>%do(df = data .frame(。))
或者,这也可以:
data.frame(x = rnorm(100))%>%do(。,df =。)
/ pre>
这个例子当然没有意义。但是在使用
group_by
时,可以将data.frame
保存为列表变量。
这是一个比较复杂的问题,似乎是相关的:
MatchIt)
n < - 5000
DF< - data.frame(
x1 = rnorm(n),
x2 = rbinom(n,1,0.5),
group = rbinom(n,1,0.5),
D = rbinom(n,1,0.5))
现在这会产生错误:
DF%>%
group_by )%>%
do(m = matchit(D〜x1,data =。,exact =x2))
但是,如果在
data.frame()
中包含。
DF%>%
group_by(group)%>%
do(m = (D〜x1,data = data.frame(。),exact =x2))
我不知道第二个例子与
matchit
是相关的,但在这两种情况下,我必须包含。
indata.frame()
。
sessionInfo()
> sessionInfo()
R版本3.1.1(2014-07-10)
平台:x86_64-apple-darwin13.4.0(64位)
语言环境:
[1] en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
附加的基本包:
[1]统计图形grDevices utils数据集方法base
其他附加包:
[1] MatchIt_2.4-21 MASS_7.3-33 dplyr_0.4.1 Defaults_1.1-1
通过命名空间加载(并未附加):
[1] assertthat_0.1 DBI_0.3.1 lazyeval_0.1.10 magrittr_1.5 parallel_3.1.1 Rcpp_0.11.4 tools_3.1.1
解决方案差异来自
magrittr
分割链的方式。expr1< - substitute(data.frame(x = rnorm(100))%> ;%do(df =。))
expr2< - substitute(data.frame(x = rnorm(100))%>%do(df =(。)))
magrittr ::: split_chain(expr1)
magrittr ::: split_chain(expr2)
Why does this not work?
data.frame(x = rnorm(100)) %>% do(df = .)
Error message:
Error in do_(.data, .dots = lazyeval::lazy_dots(...)) : argument ".data" is missing, with no default
Instead, I have to enclose the
.
indata.frame()
:data.frame(x = rnorm(100)) %>% do(df = data.frame(.))
Alternatively, this also works:
data.frame(x = rnorm(100)) %>% do(., df = .)
The example, of course, doesn't make sense. But it can be helpful to save the
data.frame
as a list variable when working withgroup_by
.Here is a more complex problem that seems to be related:
library("MatchIt") n <- 5000 DF <- data.frame( x1 = rnorm(n), x2 = rbinom(n, 1, 0.5), group = rbinom(n, 1, 0.5), D = rbinom(n, 1, 0.5))
Now this produces an error:
DF %>% group_by(group) %>% do(m = matchit(D ~ x1, data = ., exact = "x2"))
But it works when I enclose the
.
indata.frame()
:DF %>% group_by(group) %>% do(m = matchit(D ~ x1, data = data.frame(.), exact = "x2"))
I am not sure whether the second example with
matchit
is related but in both cases I have to enclose the.
indata.frame()
.sessionInfo()
> sessionInfo() R version 3.1.1 (2014-07-10) Platform: x86_64-apple-darwin13.4.0 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] MatchIt_2.4-21 MASS_7.3-33 dplyr_0.4.1 Defaults_1.1-1 loaded via a namespace (and not attached): [1] assertthat_0.1 DBI_0.3.1 lazyeval_0.1.10 magrittr_1.5 parallel_3.1.1 Rcpp_0.11.4 tools_3.1.1
解决方案The difference comes from the
magrittr
way of splitting chains.expr1 <- substitute(data.frame(x = rnorm(100)) %>% do(df = .)) expr2 <- substitute(data.frame(x = rnorm(100)) %>% do(df = (.))) magrittr:::split_chain(expr1) magrittr:::split_chain(expr2)
这篇关于为什么我有时必须在`do`中的`.``中的`.`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!