And one more extension:
# create dummy matrix set.seed(10) m <- matrix(round(runif(25, 1, 5)), 5) d <- as.data.frame(m)
If you want to assign new column names, you can do the following on data.frame :
If you, however, run the previous command on matrix , you will ruin everything:
names(m) <- LETTERS[1:5] > m [,1] [,2] [,3] [,4] [,5] [1,] 3 2 4 3 4 [2,] 2 2 3 1 3 [3,] 3 2 1 2 4 [4,] 4 3 3 3 2 [5,] 1 3 2 4 3 attr(,"names") [1] "A" "B" "C" "D" "E" NA NA NA NA NA NA NA NA NA NA NA NA NA NA [20] NA NA NA NA NA NA
Since the matrix can be thought of as a two-dimensional vector, you only give names to the first five values (you don't want to do this, do you?). In this case, you should stick with colnames() .
So there ...
aL3xa Feb 17 '10 at 18:40 2010-02-17 18:40
source share