# Attributes Example # Show examples of how to access and # change the attributes of R objects. M <- matrix(c(4.5, 2.9, 0.7, 4.0), 2, 2, byrow=T) cat('Matrix M:\n') print(M) cat('Attributes of M:\n') print(attributes(M)) # Now create a matrix with dimnames attributes. A <- matrix(c(34, 8, -2, 21), 2, 2, byrow=T, dimnames=list(c('r1', 'r2'), c('c1', 'c2'))) print(A) print(attributes(A)) # See what these matrices look like if you consider # them to be vectors. print(as.vector(M)) print(as.vector(A)) # M and A are not vectors because of their extra attributes. print(is.vector(M)) print(is.vector(A)) # We can change M and A into vectors by throwing # away their attributes. dim(M) <- NULL dim(A) <- NULL dimnames(A) <- NULL cat('Are M and A vectors?\n') cat(is.vector(M), ' ', is.vector(A), '\n') cat('Matrix M:\n') print(M) cat('Matrix A:\n') print(A)