为什么我有时必须在`do`中的`.``中的`.` [英] Why do I sometimes have to enclose `.` in `data.frame()` for a named argument in `do`?

查看:121
本文介绍了为什么我有时必须在`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 是相关的,但在这两种情况下,我必须包含 in data.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 . in data.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 with group_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 . in data.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 . in data.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屋!

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