Home > OS >  Reading specific columns and rows from excel sheet in R
Reading specific columns and rows from excel sheet in R

Time:10-02

I have a large xlsx file called Run.xlsx. Inside this are multiple sheets and I want the sheet called "Factors". I also want to extract specific rows and columns from the factors sheet which are columns Z:AB and rows 15:71.

I have tried using the readxl package however it doesn't work for me.

CodePudding user response:

If your Excel file columns order may change, it would be best to have an automatic code instead of selecting the columns number.

You could try
1- importing your xlsx file with "read.xlsx" function from "openxlsx" library
2- selecting columns with specific name

#1-import
library(openxlsx)
yourFile=read.xlsx("yourPathway/yourFile.xlsx",sheet="yourSheet")

#2-columns selection
vectorNameColumns=c("Age","BMI",...,"Gender")
vectorNameRows=15:71
refinedFile=yourFile[vectorNameRows,vectorNameColumns]

It would also be best (for safety and time consuming purpose) to automatically select specific row names or IDs instead of row numbers in the case where your Excel file would be modified or if you want to apply the same code to another Excel file.

  • Related