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"
Best Answer
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. Trynames(s_data)
. If you do usecheck.names=TRUE
, then use single back-quotes (`) to surround the names.I would also recommend using
rename
from thereshape
package (or, these days,dplyr::rename
).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.