Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R read.csv Importing Column Names Incorrectly

I have a csv that I would like to import into R as a data.frame. This csv has headers such as USD.ZeroCouponBondPrice(1m) and USD-EQ-SP500 that I can't change. When I try to import it into R, however, R's read.csv function overwrites the characters ()- as . Although I wasn't able to find a way to fix this in the function documentation, this line of code worked:

colnames(df)<-c('USD.ZeroCouponBondPrice(1m)', 'USD-EQ-SP500')

so those characters are legal in data.frame column names. Overwriting all of the column names is annoying and fragile as there are over 20 of them and it is not unthinkable for them to change. Is there a way to prevent read.csv from replacing those characters, or an alternative function to use?

like image 645
Theaetetos Avatar asked Oct 18 '17 16:10

Theaetetos


People also ask

How do you reset column names in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

How do I import a specific column into a CSV file in 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.

How do I convert column values to column names in R?

To convert a column values to column names, we can use dcast function of reshape2 package. For example, if we have a data frame called df that contains two columns say x and y, where x is categorical and y is numerical. Now if we want to convert the categories in x as column names then it can be done as dcast(df,y~x).

Can you load a CSV file in R True or false?

The CSV files can be loaded into the working space and worked using both in-built methods and external package imports. The read. csv() method in base R is used to load a .


1 Answers

If you set the argument

check.names = FALSE

in read.csv, then R will not override the names. But these names are not valid in R and they'll have to be handled differently than valid names.

like image 92
Kelli-Jean Avatar answered Sep 30 '22 15:09

Kelli-Jean