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?

• I'll leave it here, but please ask future basic R questions on StackOverflow.
– user88
Oct 10 '11 at 15:59
• Actually, the current consensus seems to be that R questions are now welcome in CV. I can't find the discussion right now, but it's somewhere in meta.stats.stackexchange.com. Aug 6 '13 at 19:53
• stackoverflow.com/questions/5788117/… Oct 13 '15 at 7:24

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.

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')]

• Answer by @Brian takes less time as compared to yours. Oct 26 '15 at 12:46

This one takes less time as compared to the provided answers

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