행렬 이름 지정하기
star_wars_matrix에 어떤 데이터가 저장되어 있는지 쉽게 파악할 수 있도록, 각 행에 영화 제목을 추가하려고 합니다. 행 이름을 추가하면 데이터를 읽기도 편하고, 행렬에서 원하는 요소를 선택할 때도 유용합니다.
벡터와 마찬가지로, 행렬의 행과 열에도 이름을 추가할 수 있습니다.
rownames(my_matrix) <- row_names_vector
colnames(my_matrix) <- col_names_vector
두 개의 벡터 region과 titles가 미리 준비되어 있습니다. 각각 star_wars_matrix의 열과 행 이름을 지정하는 데 사용하세요.
이 연습은 강의의 일부입니다
R 입문
연습 안내
colnames()를 사용하여star_wars_matrix의 열 이름을region벡터로 지정하세요.rownames()를 사용하여star_wars_matrix의 행 이름을titles벡터로 지정하세요.star_wars_matrix를 출력하여 결과를 확인하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Box office Star Wars (in millions!)
new_hope <- c(460.998, 314.4)
empire_strikes <- c(290.475, 247.900)
return_jedi <- c(309.306, 165.8)
# Construct matrix
star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE)
# Vectors region and titles, used for naming
region <- c("US", "non-US")
titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")
# Name the columns with region
# Name the rows with titles
# Print out star_wars_matrix