Thomas Lee
Thomas Lee

Reputation: 27

how to read certain row and column from list

setwd("C:\\Users\\tom24\\Desktop\\DATA TREND\\ONEMONTH")

fileNames = list.files(pattern="xlsx")
for(i in 1:length(fileNames)){
  data = read_excel(path=fileNames[i], sheet=1, col_names=FALSE)
  assign(x=fileNames[i], value=data)}

KING <- lapply(paste("C:\\Users\\tom24\\Desktop\\DATA TREND\\ONEMONTH\\",sep="",print(fileNames)),read_excel)

EXAMPLE<-KING[c(1:2500),c(1,4)]

I would like to read certain rows and columns which is [c(1:2500),c(1,4)] from all the files in the folder. However, it says incorrect number of dimensions.

Upvotes: 0

Views: 103

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

You can do this in one lapply call :

fileNames = list.files('path/to/folder/', pattern="\\.xlsx", full.names = TRUE)

lapply(fileNames, function(x) {
  readxl::read_excel(x, col_names=FALSE)[1:2500, 1:4]
}) -> result

result

Upvotes: 2

Related Questions