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
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.
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.
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.
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.
A little bit modified BondedDust version, because setnames function is not used with <- sign:
setnames(DT, make.names(colnames(DT))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With