Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Names of columns change from CSV to R: dot instead of slash

Tags:

r

csv

When I inport a CSV file in R the names of the columns change. They go from "Fe/Cu" to "Fe.Cu". But I have this mysterious problem only with one CSV file. I tried to rename the columns

colnames(a[12:ncol(a)])=c("Fe/Cu","Fe/Zn","Fe/Ba")

But nothing happens. Any ideas?

like image 487
jo.H Avatar asked Feb 01 '26 00:02

jo.H


1 Answers

Adding check.names=FALSE to csv.read function call will allow you to get the original names. This is because by default csv.read will check if column names are syntactically valid as stated by @Asayat in the commets.

From csv.read documentation:

check.names - logical: If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names. If necessary they are adjusted (by make.names) so that they are, and also to ensure that there are no duplicates.

If you then check for the documentation of make.names you will find:

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. Names such as ".2way" are not valid, and neither are the reserved words.

which is what @Asayat commented.

like image 173
Carles Mitjans Avatar answered Feb 03 '26 19:02

Carles Mitjans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!