Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Import CSV with column names that contain spaces

Tags:

CSV file looks like this (modified for brevity). Several columns have spaces in their title and R can't seem to distinguish them.

 Alias;Type;SerialNo;DateTime;Main status; [...] E1;E-70;781733;01/04/2010 11:28;8; [...] 

Here is the code I am trying to execute:

s_data <- read.csv2( file=f_name ) attach(s_data)  s_df = data.frame(                  scada_id=ID,                 plant=PlantNo,                  date=DateTime,                 main_code=Main status,                 seco_code=Additional Status,                 main_text=MainStatustext,                 seco_test=AddStatustext,                 duration=Duration)  detach(s_data) 

I have also tried substituting

main_code=Main\ status 

and

main_code="Main status" 
like image 207
klonq Avatar asked May 25 '11 12:05

klonq


People also ask

How do I refer a column name with a space in R?

A basic rule of R is to avoid naming data-frame columns using names that contain spaces. R will accept a name containing spaces, but the spaces then make it impossible to reference the object in a function.

Can there be spaces in CSV?

You can put quotes, dashes and spaces in the CSV file. Fields that contain a special character (comma, newline, or double quote), must be enclosed in double quotes.

How do I import only certain columns from a CSV file into R?

Method 1: Using read. table() function. In this method of only importing the selected columns of the CSV file data, the user needs to call the read. table() function, which is an in-built function of R programming language, and then passes the selected column in its arguments to import particular columns from the data.

Which of the following commands are used to read and write CSV files in R?

The CSV file to be read should be either present in the current working directory or the directory should be set accordingly using the setwd(…) command in R. The CSV file can also be read from a URL using read. csv() function.


2 Answers

Unless you specify check.names=FALSE, R will convert column names that are not valid variable names (e.g. contain spaces or special characters or start with numbers) into valid variable names, e.g. by replacing spaces with dots. Try names(s_data). If you do use check.names=TRUE, then use single back-quotes (`) to surround the names.

I would also recommend using rename from the reshape package (or, these days, dplyr::rename).

s_data <- read.csv2( file=f_name ) library(reshape) s_df <- rename(s_data,ID="scada_id",                PlantNo="plant",DateTime="date",Main.status="main_code",                Additional.status="seco_code",MainStatustext="main_text",                AddStatustext="seco_test",Duration="duration") 

For what it's worth, the tidyverse tools (i.e. readr::read_csv) have the opposite default; they don't transform the column names to make them legal R symbols unless you explicitly request it.

like image 67
Ben Bolker Avatar answered Sep 23 '22 15:09

Ben Bolker


s_data <- read.csv( file=f_name , check.names=FALSE) 
like image 37
Mark Avatar answered Sep 23 '22 15:09

Mark