R Language: Reading specific rows and columns in Excel files

0

You can read specific rows and columns from an Excel file in R using the `read_excel()` or `read.xlsx()` function. The following code shows how to read the first 10 rows and the first 3 columns from the Excel file `my_data.xlsx` into a data frame called `df`:

library(readxl)


df <- read_excel("my_data.xlsx", sheet = 1, range = "A1:C10")


print(df)

The `read_excel()` function also allows you to specify the rows and columns to read using a vector of row numbers and a vector of column names or numbers. For example, the following code will read the rows 2, 4, and 6 and the columns A, B, and C from the Excel file `my_data.xlsx` into a data frame called `df`:

df <- read_excel("my_data.xlsx", sheet = 1, range = c(2, 4, 6), col_names = c("A", "B", "C"))


print(df)

The `read.xlsx()` function has a similar syntax, but it uses the `startRow` and `endRow` arguments to specify the rows to read and the `colIndex` argument to specify the columns to read. For example, the following code will read the rows 2, 4, and 6 and the columns A, B, and C from the Excel file `my_data.xlsx` into a data frame called `df`:

library(xlsx)


df <- read.xlsx("my_data.xlsx", sheet = 1, startRow = 2, endRow = 6, colIndex = c(1, 2, 3))


print(df)

Post a Comment

0Comments
Post a Comment (0)