0

I have a bunch of different data frames and I want to run the same custom function to each of them. I want the output returned into each of the original data frame names, not in a list.

Let's say I have a bunch of different data frames:

mtcars1, mtcars2, mtcars3.....mtcars20. Each of these will have different factor levels on say the CYL column. I want to run a function that:

  1. returns the CYL value to a number from a factor - as.numeric(as.character())
  2. calculates some new column that is a rolling average of mpg.

Now what is the code to do that and apply this function? Again I want the output to all be mtcars1...mtcars20 data frames.

I have tried looking at

 lapply(mylist=c(mtars1...mtcars20)  function(x)    myfunct()) 

but I am running into trouble.

runningbirds
  • 6,235
  • 13
  • 55
  • 94
  • This desperately needs to be a list of data.frames. Doesn't `lapply(list(mtcars1, mtcars2, ...), myfunct)` work? – r2evans Jun 23 '16 at 00:42
  • I tried that but maybe my syntax was wrong. For a simple example pretend my function just want to convert the CYL column from a factor value to a numeric. How would I do this? I spent a while struggling with this and I couldn't get it to work so I figured there was some other way or I had the wrong approach? – runningbirds Jun 23 '16 at 00:45
  • See the following post on [working with lists of data.frames](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames). gregor's answer there has a lot of nice tips on how to work with such objects. Some of the other posts methods for putting data.frames into lists. – lmo Jun 23 '16 at 12:48

2 Answers2

0
## generate data
for (i in 1:5) assign(paste0('mtcars',i),transform(mtcars,cyl=factor(cyl+10^i)));

## collect loose data.frames into one list, and remove the originals
nms <- ls(pattern='^mtcars\\d+$');
dfs <- mget(nms);
rm(list=nms);

## solution
library(zoo); ## for rollmean()
for (i in seq_along(dfs)) {
    dfs[[i]]$cyl <- as.numeric(as.character(dfs[[i]]$cyl));
    dfs[[i]]$some.new.column <- rollmean(dfs[[i]]$mpg,5L,fill=NA);
}; ## end for
bgoldst
  • 34,190
  • 6
  • 38
  • 64
0

Can still use lapply() function to return a list of data frames:

dfs <- rep(list(mtcars), 4) #list of data frames

dfs <- lapply(dfs, function(x) {
               names(x)[1] <- "m_p_g"
               x})
lapply(dfs, head, 2)
# [[1]]
#               m_p_g cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4        21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag    21   6  160 110  3.9 2.875 17.02  0  1    4    4

# [[2]]
#               m_p_g cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4        21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag    21   6  160 110  3.9 2.875 17.02  0  1    4    4

# [[3]]
#               m_p_g cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4        21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag    21   6  160 110  3.9 2.875 17.02  0  1    4    4

# [[4]]
#               m_p_g cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4        21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag    21   6  160 110  3.9 2.875 17.02  0  1    4    4
mkearney
  • 1,266
  • 10
  • 18