必威体育Betway必威体育官网
当前位置:首页 > IT技术

R语言中 attach()与detach(),及with()的使用

时间:2019-06-21 18:42:10来源:IT技术作者:seo实验室小编阅读:89次「手机版」
 

attach

attach(what, pos = 2L, name = deparse(substitute(what), backtick=FALSE),
       warn.conflicts = TRUE)

1.attach()是对what添加路径索引,避免重复输入what名称,参数解释如下:

  • what:数据框或列表;
  • pos=2L:添加的路径存储的位置,一般默认即可。在对多个数据添加索引时,此位置会变成3L,4L,5L...detach()撤销索引路径时,会撤销对应位置的索引储存,具体例子见后;
  • name:不懂,遇见需要的情况再补充;
  • backtick=FALSE:反引号,经过测试,该参数固定为FALSE不可调,再调用索引时会用到;
  • warn.conflicts:是否打印警告。

detach()是撤销attach()建立的路径索引,往往二者配套使用。

举例如下:

> class(data) #data为自己提供的数据
[1] "data.frame"
> colnames(data) #查看data列
 [1] "No."            "Component Name" "Mass Info"      "Retention Time"
 [5] "QCRSD"          "P-value"        "Fold change"    "A-1"           
 [9] "A-2"            "A-3"            "B-1"            "B-2"           
[13] "B-3"            "QC1"            "QC2"            "QC3"           
[17] "as.is"         
> attach(data)
The following objects are masked from data (pos = 3):

    A-1, A-2, A-3, as.is, B-1, B-2, B-3, Component Name, Fold change,
    Mass Info, No., P-value, QC1, QC2, QC3, QCRSD, Retention Time

The following objects are masked from data (pos = 4):

    A-1, A-2, A-3, as.is, B-1, B-2, B-3, Component Name, Fold change,
    Mass Info, No., P-value, QC1, QC2, QC3, QCRSD, Retention Time
>No. 
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26
> A-1
Error: object 'A' not found  #因为列名“A-1”不符合变量命名规则,此处可用check.names=T改变,会 
                             #改为A.1
> `A-1`   #用反引号辅助读取A-1列信息
 [1] 6.112652e+07 5.338550e+06 9.803146e+07 1.698537e+07 3.193722e+07 2.111517e+07
 [7] 7.825601e+06 2.726090e+07 1.062785e+07 1.018187e+08 1.051693e+07 1.336919e+07
[13] 5.948830e+04 2.090497e+06 5.882181e+06 4.733337e+07 5.974494e+05 2.444981e+07
[19] 8.283025e+03 2.215970e+04 7.372889e+05 5.948745e+05 2.102290e+07 9.133244e+06
[25] 3.996773e+07 4.434691e+05
> data$`A-1`  #与上述建立路径索引结果相同
 [1] 6.112652e+07 5.338550e+06 9.803146e+07 1.698537e+07 3.193722e+07 2.111517e+07
 [7] 7.825601e+06 2.726090e+07 1.062785e+07 1.018187e+08 1.051693e+07 1.336919e+07
[13] 5.948830e+04 2.090497e+06 5.882181e+06 4.733337e+07 5.974494e+05 2.444981e+07
[19] 8.283025e+03 2.215970e+04 7.372889e+05 5.948745e+05 2.102290e+07 9.133244e+06
[25] 3.996773e+07 4.434691e+05
> summary(QC1,`A-1`)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
     4149   1194476  12129322  21224523  30429375 104915075 
> summary(data$QC1,data$`A-1`)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
     4149   1194476  12129322  21224523  30429375 104915075
> summary(`A-1`,`Fold change`)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
     8283   1075591  10572393  21472932  26558125 101818722 
> summary(data$`Fold change`,data$`A-1`)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.633   1.256   1.444   1.433   1.590   2.649
> detach(data)
> No.  #如果发现还能出结果...那就多detach几次==
Error: object 'No.' not found

2.with()函数

with(data, expr, ...)  #data可以是an environment, a list, a data frame, or an integer as in 
                       #sys.call
within(data, expr, ...)     #data为列表或数据框
within(data, expr, keepAttrs = TRUE, ...)
  • within()函数中data必须是列表或数据框,with()中data类型更广
  • 表达式,大括号{}之间的语句都只针对data执行,但如果大括号中只有一句的话,则省略大括号。
> with(data,{No.})  #No.符合变量命名标准,可直接查看
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
[17] 17 18 19 20 21 22 23 24 25 26
> with(data,{A-1})   #A-1不符合变量命名标准,不可直接查看,需要用反引号调用
Error in eval(substitute(expr), data, enclos = parent.frame()) : 
  object 'A' not found
> with(data,{`A-1`})
 [1] 6.112652e+07 5.338550e+06 9.803146e+07
 [4] 1.698537e+07 3.193722e+07 2.111517e+07
 [7] 7.825601e+06 2.726090e+07 1.062785e+07
[10] 1.018187e+08 1.051693e+07 1.336919e+07
[13] 5.948830e+04 2.090497e+06 5.882181e+06
[16] 4.733337e+07 5.974494e+05 2.444981e+07
[19] 8.283025e+03 2.215970e+04 7.372889e+05
[22] 5.948745e+05 2.102290e+07 9.133244e+06
[25] 3.996773e+07 4.434691e+05
> `A-1`    #with函数中路径索引不能在外部调用,需要用<<- 赋值才可以
Error: object 'A-1' not found
> with(data,{a-1<<-`A-1`})  #a-1符合变量命名标准,需要用反引号建立变量名
Error in a - 1 <<- `A-1` : object 'a' not found
> with(data,{a<<-`A-1`})
> a
 [1] 6.112652e+07 5.338550e+06 9.803146e+07
 [4] 1.698537e+07 3.193722e+07 2.111517e+07
 [7] 7.825601e+06 2.726090e+07 1.062785e+07
[10] 1.018187e+08 1.051693e+07 1.336919e+07
[13] 5.948830e+04 2.090497e+06 5.882181e+06
[16] 4.733337e+07 5.974494e+05 2.444981e+07
[19] 8.283025e+03 2.215970e+04 7.372889e+05
[22] 5.948745e+05 2.102290e+07 9.133244e+06
[25] 3.996773e+07 4.434691e+05
> with(data,{`a-1` <<- `A-1`})  
> a-1
 [1] 6.112652e+07 5.338549e+06 9.803146e+07
 [4] 1.698537e+07 3.193722e+07 2.111517e+07
 [7] 7.825600e+06 2.726090e+07 1.062785e+07
[10] 1.018187e+08 1.051693e+07 1.336919e+07
[13] 5.948730e+04 2.090496e+06 5.882180e+06
[16] 4.733337e+07 5.974484e+05 2.444981e+07
[19] 8.282025e+03 2.215870e+04 7.372879e+05
[22] 5.948735e+05 2.102290e+07 9.133243e+06
[25] 3.996773e+07 4.434681e+05
> with(data,{plot(No.,`A-1`)})  #{}中可以进行复杂计算

相关阅读

attachEvent报错原因

document.attachEvent is not a function出错的原因 报错原因有: (1)、利用attachEvent()直接在谷歌浏览器打开,我就是犯这种低级错

分享到:

栏目导航

推荐阅读

热门阅读