本文介绍了R 中的 attach() 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一个列表,我附上了.

i have a list in R, which i attached.

> 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

好的,我的清单真的附上了

Okay, my list is really attached

> 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 行:

grData data frame from my list has 11 rows:

> 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 行的数据框

But when i try to call grData without myList, i get data frame with 0 rows

> 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  

更新.globalEnv 中没有 grData 对象

UPD. There is no grData object in globalEnv

> ls()
[1] "checkDbLag"  "con"         "i"           "newDate"     "myList"  "updateQuery"
[7] "x" 

哪里有问题?

推荐答案

attach() 的使用实际上是很多 R 风格指南(如 http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html#attach),因为它会导致错误的结果.

The use of attach() is actually not recommended by many R style guides (like http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html#attach) because it can lead to wrong results.

正如 https 所建议的://www.r-bloggers.com/to-attach-or-not-attach-that-is-the-question/ 你有三个更好的选择:

As suggested by https://www.r-bloggers.com/to-attach-or-not-attach-that-is-the-question/ you have three better options:

  1. 直接引用变量(例如lm(ds$x ~ ds$y))
  2. 为支持此功能的命令指定数据帧(例如 lm(y ~ x, data=ds))
  3. 使用 with() 函数,它返回任何表达式的计算值(例如 with(ds,lm(y ~x)))(还要注意 within() 函数,它与 with() 类似,但返回一个修改过的对象.)
  1. Reference variables directly (e.g. lm(ds$x ~ ds$y))
  2. Specify the dataframe for commands which support this (e.g. lm(y ~ x, data=ds))
  3. Use the with() function, which returns the value of whatever expression is evaluated (e.g. with(ds,lm(y ~x)))(Also note the within() function, which is similar to with(), but returns a modified object.)

如果你仍然想使用 attach() 即使给出了所有的替代方案,我建议你提供一个完全可重现的例子.通过阅读您的代码,您似乎提供了一个不完整的输出(一次您将列表引用为 myList,另一次将其引用为 RollingMau).

If you still want to use attach() even given all the alternatives, I suggest you provide a fully reproducible example. By reading your code it seems that you are providing an incomplete output (one time you reference your list as myList, other you reference it as RollingMau).

这篇关于R 中的 attach() 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 20:37