john
john

Reputation: 707

How to read only specified column with R?

i'm trying to load a csv file. But I only need certain columns, i don't need all

This works

library(data.table)
dt <- fread("C:/test.csv", nrows=1000, select=c(1,3,5)) 

But I want to choose columns by their names, and not their sequential numbers. How can I do this?

 #this does not work
 dt <- fread("C:/test.csv", nrows=1000, select=c(col1,col3,col5))

Upvotes: 2

Views: 187

Answers (1)

Jeff
Jeff

Reputation: 81

Use the sqldf package:

library(sqldf)

dt <- read.csv.sql(file = file.choose(), sql = "SELECT col1, col2, ..., coln FROM file", header = TRUE)

Where col1, col2, ..., coln are your desired columns, and file = the file you choose from your directory.

Upvotes: 4

Related Questions