12
votes
$\begingroup$

I have an ascii dataset which consists of three columns, but only the last two are actual data. Now I want to create a dotchart of the data by using read.csv(file = "result1", sep= " "). R reads all three columns. How do I avoid this?

$\endgroup$
3
20
votes
$\begingroup$

You can use the colClasses argument to read.csv to select the columns you want. In this case, you can set colClasses to c("NULL", NA, NA)

read.csv(file="result1", sep=" ", colClasses=c("NULL", NA, NA))

More generally, you can use colClasses to specify the particular types of columns; NA means to use the default approach which is to try and figure out what the column is automatically. See the help page for read.csv for more details.

$\endgroup$
11
votes
$\begingroup$

Another option is to read in the whole file, but keep only two of the columns, e.g.:

read.csv(file = "result1", sep = " ")[ ,1:2]

or, using column names, eg. if columns are named 'col1, col2, col3'

read.csv(file = "result1", sep = " ")[ ,c('col1', 'col2')]
$\endgroup$
1
  • $\begingroup$ Answer by @Brian takes less time as compared to yours. $\endgroup$ Oct 26 '15 at 12:46
0
votes
$\begingroup$

This one takes less time as compared to the provided answers

library(data.table)
fread(file="result1", sep=" ", colClasses=c("NULL", NA, NA))
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.