You could also use JDBC to achieve this. Let's create a sample csv file.
write.table(x=mtcars, file="mtcars.csv", sep=",", row.names=F, col.names=T) # create example csv file
Download and save the the CSV JDBC driver from this link: http://sourceforge.net/projects/csvjdbc/files/latest/download and then setup the driver.
> library(RJDBC)
> path.to.jdbc.driver <- "jdbc//csvjdbc-1.0-18.jar"
> drv <- JDBC("org.relique.jdbc.csv.CsvDriver", path.to.jdbc.driver)
> conn <- dbConnect(drv, sprintf("jdbc:relique:csv:%s", getwd())) # replace getwd() with location of csv file
Let's look at the top 3 rows in the mtcars dataset:
> head(dbGetQuery(conn, "select * from mtcars"), 3)
mpg cyl disp hp drat wt qsec vs am gear carb
1 21 6 160 110 3.9 2.62 16.46 0 1 4 4
2 21 6 160 110 3.9 2.875 17.02 0 1 4 4
3 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Next, let's see the distinct values column gear takes and the respective counts:
> dbGetQuery(conn, "select gear, count(*) from mtcars group by gear")
GEAR COUNT(*)
1 4 12
2 3 15
3 5 5
Now you can compose a query using the where clause to filter on the data to only select rows where gear takes the value 5:
> dbGetQuery(conn, "select * from mtcars where gear = '5'")
mpg cyl disp hp drat wt qsec vs am gear carb
1 26 4 120.3 91 4.43 2.14 16.7 0 1 5 2
2 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
3 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4
4 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6
5 15 8 301 335 3.54 3.57 14.6 0 1 5 8