I just want to understand if there is a difference between names and colnames when working with data.frame. Both seems to behave the s
names() creates name attributes where as colnames()simply names the columns.
i.e.
Create a temp variable.
> temp <- rbind(cbind(1,2,3,4,5),
+ cbind(6,7,8,9,10))
> temp
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
Create the names.temp object.
> names.temp <- temp
Use names() on names.temp
> names(names.temp) <- paste(c("First col", "Second col", "Third col",
"Fourth Col", "Fifth col"))
> names.temp
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
attr(,"names")
[1] "First col" "Second col" "Third col" "Fourth Col" "Fifth col"
NA NA NA
[9] NA NA
We see here we can actually call the 5th name attribute in names.temp.
> names(names.temp)[5]
[1] "Fifth col"
Repeat with a second object but this time create the colnames.temp object.
> colnames.temp <- temp
Use colnames() on colnames.temp
> colnames(colnames.temp) <- paste(c("First col", "Second col", "Third col",
"Fourth Col", "Fifth col"))
> colnames.temp
First col Second col Third col Fourth Col Fifth col
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
Now name attribute is NULL.
> names(colnames.temp)[5]
NULL
FINALLY. Let's look at our trusty str() command. We can see there is a structural difference between names.temp and colnames.temp. Specifically, colnames.temp has dimnames attributes not names attributes.
> str(names.temp)
num [1:2, 1:5] 1 6 2 7 3 8 4 9 5 10
- attr(*, "names")= chr [1:10] "First col" "Second col" "Thrid col" "Fourth
Col" ...
> str(colnames.temp)
num [1:2, 1:5] 1 6 2 7 3 8 4 9 5 10
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "First col" "Second col" "Thrid col" "Fourth Col" ...