R names()

Jmnote (토론 | 기여)님의 2020년 10월 25일 (일) 18:17 판 (→‎같이 보기)

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)
##  Named num [1:48] 11506 5500 16988 2968 16 ...
##  - attr(*, "names")= chr [1:48] "Africa" "Antarctica" "Asia" "Australia" ...
names(islands)
##  [1] "Africa"           "Antarctica"       "Asia"             "Australia"       
##  [5] "Axel Heiberg"     "Baffin"           "Banks"            "Borneo"          
##  [9] "Britain"          "Celebes"          "Celon"            "Cuba"            
## [13] "Devon"            "Ellesmere"        "Europe"           "Greenland"       
## [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"

5 예제 4

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

6 같이 보기

7 참고

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