Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fread from data.table package when column names include spaces and special characters?

I have a csv file where column names include spaces and special characters.

fread imports them with quotes - but how can I change this behaviour? One reason is that I have column names starting with a space and I don't know how to handle them.

Any pointers would be helpful.

Edit: An example.

> packageVersion("data.table")
[1] ‘1.8.8’

p2p <- fread("p2p.csv", header = TRUE, stringsAsFactors=FALSE)

> head(p2p[,list(Principal remaining)])
Error: unexpected symbol in "head(p2p[,list(Principal remaining"

> head(p2p[,list("Principal remaining")])
                    V1
1: Principal remaining

> head(p2p[,list(c("Principal remaining"))])
                    V1
1: Principal remaining

What I was expecting/want is of course, what a column name without spaces yields:

> head(p2p[,list(Principal)])
   Principal
1:      1000
2:      1000
3:      1000
4:      2000
5:      1000
6:      4130
like image 335
Rico Avatar asked Jun 06 '13 16:06

Rico


People also ask

What package is Fread in in r?

table package comes with a function called fread which is a very efficient and speedy function for reading data from files. It is similar to read.

Is Fread faster than Read_csv?

csv() is actually faster than read_csv() while fread is much faster than both, although these savings are likely to be inconsequential for such small datasets.

Can Fread read CSV?

We're able to successfully import the CSV file using the fread() function. Note: We used double backslashes (\\) in the file path to avoid a common import error. Notice that we didn't have to specify the delimiter either since the fread() function automatically detected that it was a comma.

What does fread mean in R?

Its fread() function is meant to import data from regular delimited files directly into R, without any detours or nonsense. Note that “regular” in this case means that every row of your data needs to have the same number of columns.


1 Answers

A little bit modified BondedDust version, because setnames function is not used with <- sign:

setnames(DT, make.names(colnames(DT))
like image 162
dominic Avatar answered Oct 19 '22 18:10

dominic