1

I want to cut up a .csv file and reformat using R

Firstly, I have spaces in my column header names ie "Hello Joe" and if my file is DATA, I'm not sure how to deal with the file name spaces. They need to have spaces its a the way my system has been built.

R does like when I write: DATA$Hello Joe What can I do to solve this problem?

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • if you want to leave the spaces in there put backticks around the name of the column `data$\`Hello Joe\`` – Kristofersen Feb 16 '17 at 16:43
  • But when you read in those files, the headers usually get changed to `Hello.Joe`. Verify the correct names with `names(DATA)` – MrFlick Feb 16 '17 at 16:50

1 Answers1

0

To reference with $ you need to put backticks around the column names with spaces, e.g.

data$`Hello Joe`

Otherwise, if you find it easier to work without spaces you could temporarily put in an underscore and replace that with a space again later

df
  Hello Joe Hi Joe
1         1      2

> colnames(df) = gsub(" ", "_",colnames(df))
> df
  Hello_Joe Hi_Joe
1         1      2

> colnames(df) = gsub("_", " ",colnames(df))
> df
  Hello Joe Hi Joe
1         1      2
Kristofersen
  • 2,736
  • 1
  • 15
  • 31