There are two different methods in R to read specific rows from CSV file into R.

The following methods show how you can do it with syntax.

Method 1: Using read.csv() with skip Argument

df <- read.csv("data.csv",skip=n)

Method 2: Select Rows Based on Condition

library(sqldf)

df <- read.csv.sql("data.csv", sql = "condition", eol = "\n")

In above syntax read.csv.sql method is used from sqldf package.It import rows from CSV file based on provided sql condition.

In this syntax, we used the eol argument to specify “end of line” which is indicated by \n, used to break line.

Let see examples using these methods.

Suppose we have product.csv file:

Using read.csv() with skip argument

# Read rows from CSV
df <- read.csv("R studio\\product.csv",skip=2)

# Print rows
print(df)

Output:

Sensor X6
1 Display  2
2 Monitor  1

As the output shows skip first two rows and shows rows from 3rd row.

By default, R takes values from next available row as column names.

Select Rows Based on Condition

Suppose we import rows from CSV file where value in the Count column greater than 5.

# Install package
install.packages("sqldf")

# Import library
library(sqldf)

# Read rows from CSV
df <- read.csv.sql("R studio\\product.csv",
               sql="select * from file where `Count` > 5")

# Print rows
print(df)

Output:

Product Count
1 Cylinder    12
2   Sensor     6

The output shows rows having count column values greater than 5.

Using this both function you select specific rows from CSV file.