MAOC
MAOC

Reputation: 635

in R, Can I get only the names columns of a csv(txt) file?

I have many big files. But I would like get only the names of the columns without load them. Using the data.table packages, I can do

df1 <-fread("file.txt")
names1<- names(df)

But, for get all names of the all files, is ver expensive. There is some other option?

Upvotes: 0

Views: 50

Answers (1)

BarkleyBG
BarkleyBG

Reputation: 664

Many functions to read in data have optional arguments that allow you to specify how many lines you'd like to read in. For example, the read.table function would allow you to do:

df1 <- read.table("file.txt", nrows=1, header=TRUE)

colnames(df1)

I'd bet that fread() has this option too.

(Note that you may even be able to get away with nrows=0, but I haven't checked to see if that works)

EDIT

As commenter kindly points out, fread() and read.table() work a little differently.

For fread(), you'll want to supply argument nrows=0 :

df1 <- fread("file.txt", nrows=0) ##works

As per the documentation,

nrows=0 is a special case that just returns the column names and types; e.g., a dry run for a large file or to quickly check format consistency of a set of files before starting to read any.

But nrows=0 is one of the ignored cases when supplied in read.table()

df1 <- fread("file.txt") ##reads entire file    
df1 <- fread("file.txt", nrows=-1) ##reads entire file
df1 <- fread("file.txt", nrows=0) ##reads entire file

Upvotes: 2

Related Questions