0

How can I apply the same function to multiple data frames in R without having to save the data frames into a new list? I don’t want to have to type out manually the names of the data frames, that I am applying the function against. I don’t want to have to type:

data_frame2 = myfunction(data_frame2)

….Over and over. I might have 30 dataframes I want to do this too. I dont want to have to extract them out of a list.

data_frame.2 = mtcars
data_frame.111 = mtcars
data_frame.12345 = mtcars


my_function(dataset){
names(adjusted_dataset)= toupper(names(dataset))
return(adjusted_dataset)
}
my_dfs = ls(pattern = “data_frame.*”) # I know all my data frames in memory start with “data_frame”

How can I make use of sapply(my_dfs, my_function) here?

runningbirds
  • 6,235
  • 13
  • 55
  • 94
  • 2
    Any reason why you can't put all your data frames in a list to begin with, then `sapply`/`lapply` over that list, rather than having 30 different data frames as separate objects in your environment? I think many of us would *really* encourage a list-based approach – camille Jan 03 '20 at 16:56
  • Why don't you put them into a list when you create them? Anyway, you can use mget to fetch them into a list. Btw. use glob2rx to turn your wildcard pattern into a regex (which is what ls expects). – Roland Jan 03 '20 at 16:57

0 Answers0