我在R中有一个列表,我附上了。
> attach(myList)
> summary(myList)
Length Class Mode
grData 3 data.frame list
maxDate 1 POSIXct numeric
query 1 -none- character
newData 3 data.frame list
updateQuery 1 -none- function
好的,我的名单真的附上了
> search()
[1] ".GlobalEnv" "myList" "package:xlsx" "package:xlsxjars"
[5] "package:rJava" "package:quantmod" "package:TTR" "package:xts"
[9] "package:zoo" "package:tidyr" "package:stringr" "package:RPostgreSQL"
[13] "package:DBI" "package:dplyr" "tools:rstudio" "package:stats"
[17] "package:graphics" "package:grDevices" "package:utils" "package:datasets"
[21] "package:methods" "Autoloads" "package:base"
我列表中的grData 数据框有11行:
> summary(myList$grData)
date application_id value
Min. :2016-10-01 Min. : 7.0 Min. : 5769
1st Qu.:2016-10-01 1st Qu.: 9.5 1st Qu.: 33113
Median :2016-10-01 Median :12.0 Median : 65821
Mean :2016-10-01 Mean :22.0 Mean :106336
3rd Qu.:2016-10-01 3rd Qu.:37.5 3rd Qu.:108861
Max. :2016-10-01 Max. :49.0 Max. :507376
但是当我尝试在没有 myList 的情况下调用grData时,我得到的数据帧为0行
> summary(grData)
date application_id value
Min. :NA Min. : NA Min. : NA
1st Qu.:NA 1st Qu.: NA 1st Qu.: NA
Median :NA Median : NA Median : NA
Mean :NA Mean :NaN Mean :NaN
3rd Qu.:NA 3rd Qu.: NA 3rd Qu.: NA
Max. :NA Max. : NA Max. : NA
UPD。 globalEnv中没有grData对象
> ls()
[1] "checkDbLag" "con" "i" "newDate" "myList" "updateQuery"
[7] "x"
哪里可能有问题?
答案 0 :(得分:6)
许多R风格指南(例如http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html#attach)实际上并不推荐使用attach()
,因为它可能会导致错误的结果。
根据https://www.r-bloggers.com/to-attach-or-not-attach-that-is-the-question/的建议,您有三个更好的选择:
lm(ds$x ~ ds$y)
)lm(y ~ x, data=ds)
)with()
函数,该函数返回所评估的任何表达式的值(例如with(ds,lm(y ~x))
)
(另请注意within()
函数,它与with()
类似,但返回修改后的对象。)如果您仍想使用attach()
,即使给出了所有替代方案,我建议您提供完全可重现的示例。通过阅读您的代码,您似乎提供了一个不完整的输出(有一次您将列表引用为myList,另一方面您将其引用为RollingMau)。