Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

r - 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.

question from:https://stackoverflow.com/questions/65839431/how-to-read-certain-row-and-column-from-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...