what is the difference between names and colnames

前端 未结 4 952
情深已故
情深已故 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:50

    If you look at the beginning of the colnames and colnames<- functions source code :

    R> colnames
    function (x, do.NULL = TRUE, prefix = "col") 
    {
        if (is.data.frame(x) && do.NULL) 
            return(names(x))
    (...)
    
    
    R> `colnames<-`
    function (x, value) 
    {
        if (is.data.frame(x)) {
            names(x) <- value
        }
    (...)
    

    You can see that for data frames, colnames just call the names function. So yes, they are strictly equivalent.

提交回复
热议问题