작성일자 : 2023-09-29
Ver 0.1.1
- 지금까지 살펴본 객체형들과 다르게, list 객체는 상이한 자료형을 갖는 원소들을 포함할 수 있으며, 원소의 길이도 동일할 필요가 없다.
- List의 원소를 성분(component)이라고 부른다.
List 만들기
mat1 = matrix( 1:4 , nrow = 2 )
list1 = list( 'A' , 1:8 , mat1 )
list1
## [[1]]
## [1] "A"
##
## [[2]]
## [1] 1 2 3 4 5 6 7 8
##
## [[3]]
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
student = list( student.name = c( 'Jake' , 'Jason' , 'Ashley' ),
student.cnt = 3 ,
student.age = c( 20 , 21 , 19 ) )
student
## $student.name
## [1] "Jake" "Jason" "Ashley"
##
## $student.cnt
## [1] 3
##
## $student.age
## [1] 20 21 19
List의 특성
mode( list1 )
## [1] "list"
length( list1 ) # 성분의 개수
## [1] 3
names( list1 ) # 성분의 이름 (list1은 성분의 이름이 정의되지 않음)
## NULL
mode( student ) # list의 mode는 항상 list로 주어짐
## [1] "list"
length( student )
## [1] 3
names( student )
## [1] "student.name" "student.cnt" "student.age"
List 의 원소 추출
list1[[ 2 ]]
## [1] 1 2 3 4 5 6 7 8
list1[ 2 ]
## [[1]]
## [1] 1 2 3 4 5 6 7 8
list1[[ 3 ]][ 1 , 2 ]
## [1] 3
student$student.name
## [1] "Jake" "Jason" "Ashley"
student[[ 1 ]]
## [1] "Jake" "Jason" "Ashley"
student[[ 'student.name' ]]
## [1] "Jake" "Jason" "Ashley"
student[ 'student.name' ]
## $student.name
## [1] "Jake" "Jason" "Ashley"
student$student.name[ 1 ]
## [1] "Jake"
성분에 이름 붙이기
names( list1 ) = c( 'character' , 'vector' , 'matrix' )
list1
## $character
## [1] "A"
##
## $vector
## [1] 1 2 3 4 5 6 7 8
##
## $matrix
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
성분 추가하기
list1[[ 4 ]] = letters[ 1:4 ] # adding 4th component
list1[ 5 ] = list( c( T , F ) ) # adding 5th component
list1[[ 2 ]][ 9 ] = 9 # an element is added to 2nd component
list1
## $character
## [1] "A"
##
## $vector
## [1] 1 2 3 4 5 6 7 8 9
##
## $matrix
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## [[4]]
## [1] "a" "b" "c" "d"
##
## [[5]]
## [1] TRUE FALSE
student$student.lastname = c( 'Lee', 'Cui' , 'Choi' )
student = c( student , list( student.grade = c( 100 , 87 , 96 ) ) )
student$description = "This is the class information"
student
## $student.name
## [1] "Jake" "Jason" "Ashley"
##
## $student.cnt
## [1] 3
##
## $student.age
## [1] 20 21 19
##
## $student.lastname
## [1] "Lee" "Cui" "Choi"
##
## $student.grade
## [1] 100 87 96
##
## $description
## [1] "This is the class information"
성분 제거하기
list1[ 5 ] = NULL # deleting 5th component
list1[[ 4 ]] = NULL # deleting 4th component
list1[[ 2 ]] = list1[[ 2 ]][ -9 ] # deleting 9th element of 2nd component
list1
## $character
## [1] "A"
##
## $vector
## [1] 1 2 3 4 5 6 7 8
##
## $matrix
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
student$student.grade = NULL
student[['student.lastname']] = NULL
student
## $student.name
## [1] "Jake" "Jason" "Ashley"
##
## $student.cnt
## [1] 3
##
## $student.age
## [1] 20 21 19
##
## $description
## [1] "This is the class information"
List의 성분으로써 list 사용하기
Jake = list( first.name = 'Jake', middle.name = 'J.' , last.name = 'Lee' , age = 20 , sex = 'M' , grade = 'A' )
Jason = list( first.name = 'Jason' , middle.name = NULL , last.name = 'Cui' , age = 21 , sex = 'M' , grade = 'B' )
Ashley = list( first.name = 'Ashley' , middle.name = NULL , last.name = 'Choi' , age = 19 , sex = 'F' , grade = 'A-' )
my.class = list( student.list = list( st1 = Jake , st2 = Jason , st3 = Ashley ) )
my.class$cnt = length( my.class$student.list )
my.class
## $student.list
## $student.list$st1
## $student.list$st1$first.name
## [1] "Jake"
##
## $student.list$st1$middle.name
## [1] "J."
##
## $student.list$st1$last.name
## [1] "Lee"
##
## $student.list$st1$age
## [1] 20
##
## $student.list$st1$sex
## [1] "M"
##
## $student.list$st1$grade
## [1] "A"
##
##
## $student.list$st2
## $student.list$st2$first.name
## [1] "Jason"
##
## $student.list$st2$middle.name
## NULL
##
## $student.list$st2$last.name
## [1] "Cui"
##
## $student.list$st2$age
## [1] 21
##
## $student.list$st2$sex
## [1] "M"
##
## $student.list$st2$grade
## [1] "B"
##
##
## $student.list$st3
## $student.list$st3$first.name
## [1] "Ashley"
##
## $student.list$st3$middle.name
## NULL
##
## $student.list$st3$last.name
## [1] "Choi"
##
## $student.list$st3$age
## [1] 19
##
## $student.list$st3$sex
## [1] "F"
##
## $student.list$st3$grade
## [1] "A-"
##
##
##
## $cnt
## [1] 3
mode( my.class$student.list )
## [1] "list"
unlist() 함수
student
## $student.name
## [1] "Jake" "Jason" "Ashley"
##
## $student.cnt
## [1] 3
##
## $student.age
## [1] 20 21 19
##
## $description
## [1] "This is the class information"
unlist( student )
## student.name1 student.name2
## "Jake" "Jason"
## student.name3 student.cnt
## "Ashley" "3"
## student.age1 student.age2
## "20" "21"
## student.age3 description
## "19" "This is the class information"
is.list( student )
## [1] TRUE
mat1
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
is.list( mat1 )
## [1] FALSE
as.list( mat1 )
## [[1]]
## [1] 1
##
## [[2]]
## [1] 2
##
## [[3]]
## [1] 3
##
## [[4]]
## [1] 4