Search code examples
rdataframereserved-words

Create a dataframe with a reserved word as column name


I tried the 2 ways below and it seems that whatever I do, a dot is added at the end of the name of the column "repeat":

df <- data.frame(col1=1:5,col2=6:10,"repeat"=11:15)

df <- data.frame(col1=1:5,col2=6:10,`repeat`=11:15)

df

enter image description here

Is there a way to do force it? Thanks!


Solution

  • After inspecting > data.frame, I found the solution (avoid that the names are checked):

    df <- data.frame(col1=1:5,col2=6:10, 'repeat' = 11:15, check.names=FALSE)
    df
    ##   col1 col2 repeat
    ## 1    1    6     11
    ## 2    2    7     12
    ## 3    3    8     13
    ## 4    4    9     14
    ## 5    5   10     15
    

    An alternative is renaming of the wrongly named df:

    df <- data.frame(col1=1:5,col2=6:10, "repeat" = 11:15)
    names(df) <- c("col1", "col2", "repeat")
    df
    ##   col1 col2 repeat
    ## 1    1    6     11
    ## 2    2    7     12
    ## 3    3    8     13
    ## 4    4    9     14
    ## 5    5   10     15