Tools/R

R - (10) 리스트(List)

2017. 3. 4. 01:49
반응형

(10) 리스트 (List)


List 만들기


List와 Vector의 차이점은 Vector는 한 가지 타입의 원소만 담을 수 있지만, List는 어떤 타입이든 담을 수 있다는 것이다.  List는 위와 같이 list() 키워드를 통해 만들 수 있다.


# Create a list containing strings, numbers, vectors and a logical values.
list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(list_data)



[[1]]
[1] "Red"

[[2]]
[1] "Green"

[[3]]
[1] 21 32 11

[[4]]
[1] TRUE

[[5]]
[1] 51.23

[[6]]
[1] 119.1


출력해보면 위와 같이 첫 번째 원소는 String, 세 번째 원소는 Vector, 네 번째 원소는 진릿값인 것을 알 수 있다.



List에 이름 붙이기 


# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
   list("green",12.3))

# Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

# Show the list.
print(list_data)


$`1st Quarter`
[1] "Jan" "Feb" "Mar"

$A_Matrix
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8

$`A Inner list`
$`A Inner list`[[1]]
[1] "green"

$`A Inner list`[[2]]
[1] 12.3



위와 같은 문법을 통해 List의 각각의 원소에 이름을 붙일 수 있다. 



리스트 원소 접근


리스트에는 기본적으로 index를 통해 접근할 수 있고, 이름이 붙어져 있으면 이름으로도 접근할 수 있다. 이름으로 접근할 때는 $키워드를 통해 리스트의 이름을 명시해주면 된다.


# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
   list("green",12.3))

# Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

# Access the first element of the list.
print(list_data[1])

# Access the thrid element. As it is also a list, all its elements will be printed.
print(list_data[3])

# Access the list element using the name of the element.
print(list_data$A_Matrix)


$`1st Quarter`
[1] "Jan" "Feb" "Mar"

$`A Inner list`
$`A Inner list`[[1]]
[1] "green"

$`A Inner list`[[2]]
[1] 12.3


     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8



리스트 원소 조작하기 (Manipulating)


리스트 원소 조작은 아래와 같이 <- 키워드를 통해 새로운 값을 넣어주면 된다.


# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
   list("green",12.3))

# Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

# Add element at the end of the list.
list_data[4] <- "New element"
print(list_data[4])

# Remove the last element.
list_data[4] <- NULL

# Print the 4th Element.
print(list_data[4])

# Update the 3rd Element.
list_data[3] <- "updated element"
print(list_data[3])

[[1]]
[1] "New element"

$<NA>
NULL

$`A Inner list`
[1] "updated element"


리스트 합치기


아래와 같이 c() 키워드를 통해 리스트를 합칠 수 있다. 


# Create two lists.
list1 <- list(1,2,3)
list2 <- list("Sun","Mon","Tue")

# Merge the two lists.
merged.list <- c(list1,list2)

# Print the merged list.
print(merged.list)


[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] "Sun"

[[5]]
[1] "Mon"

[[6]]
[1] "Tue"


리스트를 벡터로 변환하기


List 상태에서는 벡터 상태에서 보다 조작의 자유도가 떨어진다. 예를 들어 산술 연산을 한다거나 할 수 없다. 다양한 조작을 위해 리스트를 벡터로 변환할 때는 아래와 같이 unlist() 키워드를 이용하면 된다. 


# Create lists.
list1 <- list(1:5)
print(list1)

list2 <-list(10:14)
print(list2)

# Convert the lists to vectors.
v1 <- unlist(list1)
v2 <- unlist(list2)

print(v1)
print(v2)

# Now add the vectors
result <- v1+v2
print(result)


[[1]]
[1] 1 2 3 4 5

[[1]]
[1] 10 11 12 13 14

[1] 1 2 3 4 5
[1] 10 11 12 13 14
[1] 11 13 15 17 19



아래 튜토리얼을 참고한 포스팅입니다.


https://www.tutorialspoint.com/r/r_lists.htm

반응형
반응형