Assigning colnames() to specific column of a data frame

前端 未结 3 462
梦谈多话
梦谈多话 2021-01-14 11:22

I have a question about the colnames function in the base package

Let\'s say you have a data.frame, as follows:

df <- data.frame(var         


        
相关标签:
3条回答
  • 2021-01-14 11:24

    Call the colnames() function on the dataframe (the entire dataframe) then access by indexing, the items of the 1D vector returned by that function call:

    > data(Orange)    
    > Orange[1:5,]
      Tree  age circumference
    1    1  118            30
    2    1  484            58
    3    1  664            87
    4    1 1004           115
    5    1 1231           120
    > call *colnames* on the Orange dataframe and bind it to the variable *cn*
    > cn = colnames(Orange)
    > cn    
    [1] "Tree"          "age"           "circumference"
    > length(cn)
    [1] 3
    > class(cn)
     [1] "character"
    
    > # access the items of this 1D character vector by index:
    > cn[1]
    [1] "Tree"
    > cn[3]
    [1] "circumference"
    > # likewise modify any item the same way:
    > cn[3] = '2*pi*r'
    
    0 讨论(0)
  • 2021-01-14 11:28

    Because you should be doing this:

    > colnames(df)[1] <- "test"
    > colnames(df)[1]
    [1] "test"
    

    The colnames function returns a character vector that can be altered.

    0 讨论(0)
  • 2021-01-14 11:39

    The reason your version does not do what you expect is that df[1] creates a temporary data frame in memory, the colnames function then changes the name of the 1 column in this temporary data frame (not your original data frame), but then nothing else is done with the temporary df so it is silently discarded. Your original data frame was never touched, so the next time you do colnames(df[1]) a new temporary df is created copying from your unmodified original and the colname is returned.

    Changing the order of calling colnames and subsetting does what you want as the other answers show.

    0 讨论(0)
提交回复
热议问题