R语言中 attach()与detach(),及with()的使用
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 TimeThe 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`)}) #{}中可以进行复杂计算
R语言中 attach()与detach(),及with()的使用相关推荐
- R语言中的attach()与detach()
R语言中的attach()与detach() 我是学习R语言的新手,新手嘛只能做一只搬运知识的小蚂蚁,但我也是一只认真的小蚂蚁,所有的分享都是在自己遇到的问题基础上,结合前人的学习自己总结过的.这是我 ...
- 1071svm函数 r语言_如何利用R语言中的rpart函数建立决策树模型
决策树是根据若干输入变量的值构造出一个适合的模型,以此来预测输出变量的值,并用树形结构展示出来.决策树主要有两个类别:分类树和回归树.分类树主要针对离散的目标变量,回归树则针对连续的目标变量.R语言中 ...
- r语言中c函数错误,R语言中c()函数与paste()函数的区别说明
c()函数:将括号中的元素连接起来,并不创建向量 paste()函数:连接括号中的元素 例如 c(1, 2:4),结果为1 2 3 4 paste(1, 2:4),结果为"1 2" ...
- r语言中的shiny教程_如何使用Shiny在R中编写Web应用程序
r语言中的shiny教程 新年快乐! 这个月我忙于撰写一些较大的文章,因此请在接下来的几周内查找这些文章. 对于本月的Nooks和Crannies,我想简要指出一个我一直在用它进行自我教育的出色R库. ...
- R语言中if语句使用方法之超详细教程
在R语言中,if属于一种分支结构,即根据某个条件执行相关的语句.R中的if语句与else配合主要有3种结构. 单个if语句 if(cond) {expr} 其它语句 即当括弧中的cond条件为TRUE ...
- R语言中GCC编译的问题(续)
这篇文章承接R语言中GCC编译的问题,这篇文章主要解决我在Linux系统上安装"expm"出现的问题. 出现的问题 这个问题非常的有趣,因为我在两台服务器分别安装同一个包,其中一台 ...
- r语言中paste函数_R中的paste()函数-简要指南
r语言中paste函数 Using the paste() function in R will be straight and simple. In this tutorial let's see ...
- r语言中的while循环_R编程中的While循环
r语言中的while循环 In addition to the for loop we discussed earlier, R also offers another kind of loop to ...
- R语言中的igraph包绘制网络图
本文转自网络. R语言中的igraph包可以很方便地画出网络图,在社交关系分析等领域发挥重要作用,下面介绍包中一个重要的函数graph_from_data_frame(). graph_from_da ...
- 相关系数pearson、spearman、kendall和R语言中的cor/or.test()
相关系数pearson.spearman.kendall和R语言中的cor/cor.test 1. 相关系数pearson.spearman.kendall 2. R语言cor函数和cor.test函 ...
最新文章
- golang 写入文件 覆盖旧文件 将旧文件清空
- Java判断一个数是否是回文数
- socket 编程:回射客户/服务程序
- 安装debian 9.1后,中文环境下将home目录下文件夹改为对应的英文
- apache-hive-3.1.0-bin.tar.gz 下载
- S3C64xx设备树支持
- 编码:隐匿在计算机软硬件背后的语言
- python制作折线图_python 生成折线图
- mysql 一个月以前_mysql查询昨天 一周前 一月前 一年前的数据
- 小程序日期(日历)时间 选择器组件
- Be awesome
- FLASH脚本基础入门讲解1
- 《华为机试》刷题之HJ84 统计大写字母个数
- Artetxe2018CoNLL_Uncovering divergent linguistic information in word embeddings...
- Java题目训练——星际密码和数根
- 时尚与森系的碰撞,这样的妆容造型别样美!
- (附源码)计算机毕业设计ssm房地产销售系统
- 计算机应用文摘杂志影响因子,计算机应用文摘杂志
- ext2fsd使用总结
- 一款免费的百度网盘下载软件