create a data frame from specific rows/columns from every .xlsx file in a folder

Hello, I would like to create a data frame that takes a specific row across several columns from every .xlsx file within a folder. If the dataframe could also include the name of the file in a column too that would be great. So far I have this:

library(readxl)
setwd("~/Documents/Kent/Students/FYP/2020-21/SRE_loneliness/data/OlderAdults/data")
file.list <- list.files(pattern='*.xlsx')
df.list <- lapply(file.list, read_excel)

Let's say you want to keep all the rows where Name == "A" and the columns Name and Value. I would use code like the following.

# Your code
library(readxl)
setwd("~/Documents/Kent/Students/FYP/2020-21/SRE_loneliness/data/OlderAdults/data")
file.list <- list.files(pattern='*.xlsx')
df.list <- lapply(file.list, read_excel)

 #Added code
library(dplyr)
library(purrr)
names(df.list) <- file.list
FilterFunc <- function(DF) filter(DF, Name == "A") |> select(Name, Value)
map(df.list, FilterFunc) |> bind_rows(.id = "File")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.