5.1.2 데이터 차원 조사하기
행렬, 배열, 데이터 프레임 모두 클래스와 타입 외에 차원과 관련한 속성을 갖고 있다.
데이터 차원 구하기
R에서 벡터는 1차원 데이터 구조를 갖는다.
> vec <- c(1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6) > class(vec) [1] "numeric" > typeof(vec) [1] "double"
2차원 데이터는 dim(), nrow(), ncol() 함수를 활용할 수 있다.
> sample_matrix <- matrix(vec, ncol = 4) > sample_matrix [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 2 3 4 5 [3,] 3 4 5 6 > class(sample_matrix) [1] "matrix" > typeof(sample_matrix) [1] "double" > dim(sample_matrix) [1] 3 4 > nrow(sample_matrix) [1] 3 > ncol(sample_matrix) [1] 4