Create column names with spaces

Asked

Viewed 73 times

0

I’m trying to create a DF with columns that contain spaces in their names, but an error is appearing. This is the DF I’m trying to create:

MUNICIPIO <- c('BELO HORIZONTE','BRASILIA','JUIZ DE FORA','MANAUS','MONTES CLAROS','RECIFE','RIO DE JANEIRO','SALVADOR','SAO PAULO','UBERLANDIA')
BELO HORIZONTE <- c(0,700,250,1000,400,800,430,900,500,30)

This is error message:

Error: unexpected symbol in "BELO HORIZONTE"

How do I solve this example of my problem? The DF I am working on has several columns like this example, so I would like a solution suggestion that could cover several columns

1 answer

2


One way to solve this is to write the variable name between bass accents:

MUNICIPIO <- c('BELO HORIZONTE', 'BRASILIA', 'JUIZ DE FORA', 'MANAUS', 
               'MONTES CLAROS', 'RECIFE', 'RIO DE JANEIRO', 'SALVADOR', 
               'SAO PAULO', 'UBERLANDIA')
`BELO HORIZONTE` <- c(0, 700, 250, 1000, 400, 800, 430, 900, 500, 30)
BRASILIA <- c(700, 0, 350, 1200, 50, 110, 780, 984, 1150, 5)
`JUIZ DE FORA` <- c(250, 350, 0, 200, 15, 260, 305, 412, 29, 102)
MANAUS <- c(1000, 1200, 200, 0, 77, 115, 225, 318, 412, 511)
`MONTES CLAROS` <- c(400, 50, 15, 77, 0, 88, 819, 733, 978, 1001)
RECIFE <- c(800, 110, 260, 115, 88, 0, 17, 3000, 1418, 735)
`RIO DE JANEIRO` <- c(430, 780, 305, 225, 819, 17, 0, 513, 701, 56)
SALVADOR <- c(900, 984, 412, 318, 733, 3000, 513, 0, 389, 499)
`SAO PAULO` <- c(500, 1150, 29, 412, 978, 1418, 701, 389, 0, 1113)
UBERLANDIA <- c(30, 5, 102, 511, 1001, 735, 56, 499, 1113, 0)
DADOS_2 <- data.frame(MUNICIPIO, `BELO HORIZONTE`, BRASILIA, `JUIZ DE FORA`,
                      MANAUS, `MONTES CLAROS`, RECIFE, `RIO DE JANEIRO`, 
                      SALVADOR, `SAO PAULO`, UBERLANDIA)

I don’t know exactly why I’m doing this, because R will lose space information if a data frame is created with these vectors:

names(DADOS_2)
 [1] "MUNICIPIO"      "BELO.HORIZONTE" "BRASILIA"       "JUIZ.DE.FORA"   "MANAUS"        
 [6] "MONTES.CLAROS"  "RECIFE"         "RIO.DE.JANEIRO" "SALVADOR"       "SAO.PAULO"     
[11] "UBERLANDIA"

If the goal is to use these names of cities with space for some presentation of results, I recommend keeping them all without space in the names of the columns and in the objects within Workspace. Let to manually set these names only in the actual presentation, as for example in the Abels of possible graphics.

  • I will answer, but I will need the name with space. The process I am building requires me this way, since I will check the distances between cities (matrix), IE, each column will be a city, as my example above (soon I will publish a post with this doubt). Anyway, at this first moment I need the name with the space.

  • 1

    You can use the accents and work with the package tibble, he’s an improved version of data.frame. It improves the presentation when you call it and also keeps the names separate. Just follow the example of @Marcus-Nunes and swap data.frame for tibble. And first download the package tibble if you don’t have it, it comes with the tidyverse also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.