속성
강의의 마지막 연습 문제까지 오셨어요! 축하드립니다! 이번에는 가볍게 마무리해 볼게요.
속성(attribute)은 데이터 구조에 대한 추가 메타데이터예요. 자주 쓰이는 속성으로는 행 이름과 열 이름, 차원, 그리고 클래스가 있어요. attributes() 함수는 전달한 객체의 속성을 리스트로 반환합니다. 특정 속성만 확인하려면 attr() 함수를 사용할 수 있어요.
cash의 속성을 살펴보면:
attributes(cash)
$names
[1] "company" "cash_flow" "year"
$row.names
[1] 1 2 3 4 5 6 7
$class
[1] "data.frame"
attr(cash, which = "names")
[1] "company" "cash_flow" "year"
이 연습은 강의의 일부입니다
금융을 위한 R 입문
연습 안내
- 행렬
my_matrix와 요인my_factor가 미리 정의되어 있어요. my_matrix에attributes()를 사용하세요.my_matrix에서"dim"속성을 반환하도록attr()를 사용하세요.my_factor에attributes()를 사용하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# my_matrix and my_factor
my_matrix <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3)
rownames(my_matrix) <- c("Row1", "Row2")
colnames(my_matrix) <- c("Col1", "Col2", "Col3")
my_factor <- factor(c("A", "A", "B"), ordered = T, levels = c("A", "B"))
# attributes of my_matrix
# Just the dim attribute of my_matrix
# attributes of my_factor