what is the difference between names and colnames

前端 未结 4 949
情深已故
情深已故 2020-12-08 14:01

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

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 14:45

    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" ...
    

提交回复
热议问题