Renaming columns that are showing as a number

I am a complete newby to Rstudio and I am struggling with renaming column names where the column is a number.

I can use ashes_match[,3] to display the column but when I try to rename it I am having difficulties.
The column names are from a combined set of data with the last four being the second data set.
1] "batter" "team" "role" "Innings" "description" "1" "2" "3"
[9] "4"

I am trying to rename column 3 to scored without success. I have used the following:

rename(ashes_match, "scored" = "[,3]")

this is the error message
Error in UseMethod("rename") :
no applicable method for 'rename' applied to an object of class "c('matrix', 'array', 'character')"

any help would be appreciated

To do this, you need to use backticks


ashes_match <- tibble(batter=1:2, team=letters[1:2], `1`=3:4, `3`=5:6)

rename(ashes_match, scored = `3`)
#> # A tibble: 2 x 4
#>   batter team    `1` scored
#>    <int> <chr> <int>  <int>
#> 1      1 a         3      5
#> 2      2 b         4      6

Created on 2021-03-24 by the reprex package (v1.0.0)

1 Like

Just to give you a little background as to why, you have to surround column names that are not “syntactically valid” in R in backticks.

From the documentation for the base R function, make.names():

A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number.

Any of R's reserved words are also non-syntactic names (i.e. to use them, you'll need to surround them in backticks).

1 Like

Many thanks StatSteph for replying so quickly, I'll try this later tonight.


Thanks Mara for the clarification


1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.