"R names()"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 18개는 보이지 않습니다)
3번째 줄: 3번째 줄:
* 객체에 대한 이름들을 얻거나(get) 설정하는(set) R 함수
* 객체에 대한 이름들을 얻거나(get) 설정하는(set) R 함수


<source lang='r'>
==예제 1==
<syntaxhighlight lang='r'>
z <- 1:5
z
## [1] 1 2 3 4 5
 
names(z) <- letters[1:5]
z
## a b c d e
## 1 2 3 4 5
names(z[3])
## [1] "c"
names(z[3:5])
## [1] "c" "d" "e"
names(z[c(3,5)])
## [1] "c" "e"
</syntaxhighlight>
<syntaxhighlight lang='r'>
z <- 1:5
class(z)
## [1] "integer"
str(z)
##  int [1:5] 1 2 3 4 5
 
names(z) <- letters[1:5]
class(z)
## [1] "integer"
str(z)
##  Named int [1:5] 1 2 3 4 5
##  - attr(*, "names")= chr [1:5] "a" "b" "c" "d" ...
 
names(z) <- NULL
class(z)
## [1] "integer"
str(z)
##  int [1:5] 1 2 3 4 5
</syntaxhighlight>
 
==예제 2==
<syntaxhighlight lang='r'>
z <- letters[1:5]
z
## [1] "a" "b" "c" "d" "e"
 
names(z) <- 1:5
z
##  1  2  3  4  5
## "a" "b" "c" "d" "e"
 
names(z) <- NULL
z
[1] "a" "b" "c" "d" "e"
## [1] "a" "b" "c" "d" "e"
</syntaxhighlight>
<syntaxhighlight lang='r'>
z <- letters[1:5]
class(z)
## [1] "character"
str(z)
##  chr [1:5] "a" "b" "c" "d" "e"
 
names(z) <- 1:5
class(z)
## [1] "character"
str(z)
##  Named chr [1:5] "a" "b" "c" "d" "e"
##  - attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...
 
names(z) <- NULL
class(z)
## [1] "character"
str(z)
##  chr [1:5] "a" "b" "c" "d" "e"
</syntaxhighlight>
 
==예제 3==
<syntaxhighlight lang='r' notebook=islands>
str(islands)
str(islands)
##  Named num [1:48] 11506 5500 16988 2968 16 ...
</syntaxhighlight>
##  - attr(*, "names")= chr [1:48] "Africa" "Antarctica" "Asia" "Australia" ...
<syntaxhighlight lang='r' notebook=islands>
names(islands)
names(islands)
##  [1] "Africa"          "Antarctica"      "Asia"            "Australia"     
</syntaxhighlight>
##  [5] "Axel Heiberg"    "Baffin"          "Banks"            "Borneo"         
 
##  [9] "Britain"          "Celebes"          "Celon"            "Cuba"           
==예제 4==
## [13] "Devon"            "Ellesmere"        "Europe"          "Greenland"     
<syntaxhighlight lang='r' notebook>
## [17] "Hainan"          "Hispaniola"      "Hokkaido"        "Honshu"         
## [21] "Iceland"          "Ireland"          "Java"            "Kyushu"         
## [25] "Luzon"            "Madagascar"      "Melville"        "Mindanao"       
## [29] "Moluccas"        "New Britain"      "New Guinea"      "New Zealand (N)"
## [33] "New Zealand (S)"  "Newfoundland"    "North America"    "Novaya Zemlya" 
## [37] "Prince of Wales"  "Sakhalin"        "South America"    "Southampton"   
## [41] "Spitsbergen"      "Sumatra"          "Taiwan"          "Tasmania"       
## [45] "Tierra del Fuego" "Timor"            "Vancouver"        "Victoria"   
</source>
<source lang='r'>
z <- list(a = 1, b = "c", c = 1:3)
z <- list(a = 1, b = "c", c = 1:3)
z
z
## $a
</syntaxhighlight>
## [1] 1
<syntaxhighlight lang='r' notebook>
##
## $b
## [1] "c"
##
## $c
## [1] 1 2 3
names(z)
names(z)
## [1] "a" "b" "c"
</syntaxhighlight>
<syntaxhighlight lang='r' notebook>
names(z[3])
names(z[3])
## [1] "c"
</syntaxhighlight>
</source>


==같이 보기==
==같이 보기==
* [[R slotNames()]]
* [[R slotNames()]]
* [[R dimnames()]]
* [[R dimnames()]]
* [[R setNames()]]
* [[R 데이터프레임 컬럼명 변경]]


==참고==
==참고==

2021년 8월 22일 (일) 16:23 기준 최신판

1 개요[ | ]

R names()
  • 객체에 대한 이름들을 얻거나(get) 설정하는(set) R 함수

2 예제 1[ | ]

z <- 1:5
z
## [1] 1 2 3 4 5

names(z) <- letters[1:5]
z
## a b c d e 
## 1 2 3 4 5
names(z[3])
## [1] "c"
names(z[3:5])
## [1] "c" "d" "e"
names(z[c(3,5)])
## [1] "c" "e"
z <- 1:5
class(z)
## [1] "integer"
str(z)
##  int [1:5] 1 2 3 4 5

names(z) <- letters[1:5]
class(z)
## [1] "integer"
str(z)
##  Named int [1:5] 1 2 3 4 5
##  - attr(*, "names")= chr [1:5] "a" "b" "c" "d" ...

names(z) <- NULL
class(z)
## [1] "integer"
str(z)
##  int [1:5] 1 2 3 4 5

3 예제 2[ | ]

z <- letters[1:5]
z
## [1] "a" "b" "c" "d" "e"

names(z) <- 1:5
z
##   1   2   3   4   5 
## "a" "b" "c" "d" "e" 

names(z) <- NULL
z
[1] "a" "b" "c" "d" "e"
## [1] "a" "b" "c" "d" "e"
z <- letters[1:5]
class(z)
## [1] "character"
str(z)
##  chr [1:5] "a" "b" "c" "d" "e"

names(z) <- 1:5
class(z)
## [1] "character"
str(z)
##  Named chr [1:5] "a" "b" "c" "d" "e"
##  - attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...

names(z) <- NULL
class(z)
## [1] "character"
str(z)
##  chr [1:5] "a" "b" "c" "d" "e"

4 예제 3[ | ]

str(islands)
names(islands)

5 예제 4[ | ]

z <- list(a = 1, b = "c", c = 1:3)
z
names(z)
names(z[3])

6 같이 보기[ | ]

7 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}