In the R language, you can use attach or with when you are working with data in a string column, in order to avoid having to type the object name repeatedly.
1, attach ()
Suppose data.frame contains columns Name,age
Attach (Onedata.frame), you can refer to the elements in the onedata.frame directly, for example:
(1) Create test data frame
> name<-c ("Zhangshan", "Lisi", "Wangwu", "Zhaoliu")
> age<-c (20,30,40,50)
> Onedata.frame<-data.frame (name,age)
> Onedata.frame
Name age
1 Zhangshan 20
2 Lisi 30
3 Wangwu 40
4 Zhaoliu 50
(2) Attach test
> Attach (Onedata.frame)
The following objects is masked _by_. Globalenv:
Age, name
> Age
[1] 20 30 40 50
> Name
[1] "Zhangshan" "Lisi" "Wangwu" "Zhaoliu"
> Detach (Onedata.frame)
> Name
Error: Object ' name ' not found
It is visible that the elements in the access data frame can be searched only between command attach () and detach ().
2. With ()
Using with, like with in JavaScript, is valid in parentheses. For example:
>with (onedata.frame,{
Name
})
[1] Zhangshan Lisi Wangwu Zhaoliu
Levels:lisi Wangwu Zhangshan Zhaoliu
The problem with using with is that the variables set inside cannot be accessed externally:
> with (Onedata.frame,{name1<-name})
> name1
Error: Object ' name1 ' not found
The solution is to use the <<-assignment notation, for example:
> with (Onedata.frame,{name1<<-name})
> name1
[1] Zhangshan Lisi Wangwu Zhaoliu
Levels:lisi Wangwu Zhangshan Zhaoliu
>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
R language-attach, detach, with