首页 > 解决方案 > Reading specific columns and rows from excel sheet in R

问题描述

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.

标签: rexcel

解决方案


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.


推荐阅读