0

I have dataframe mydf and I've split it to get number of data frames like mydf$'1' mydf$'2'...mydf$'n'. I wish to apply my function on these data frames to get resultdf like

resultdf1 <- myfunc(mydf$'1')
resultdf2 <- myfunc(mydf$'2')
.....
resultdfn <- myfunc(mydf$'n')

and so on...Since there are about 35 data frames from the split. How can I reduce my effort without manually writing for each frame name and resultdf.

I tried lapply but didn't work out correctly. Any help will be appreciated.

Thanks

Tarak
  • 775
  • 2
  • 6
  • 12

1 Answers1

3

Calling split on a data frame should give you a list of smaller data frames. So perhaps it would just make sense to call lapply on this split list, e.g.

mydf.split <- split(mydf, mydf$col)
list.result <- lapply(mydf.split, function(x) { /* do something */ })

The above would yield another list, with each node containing the result of your function. If you want to get back a vector instead, you could use sapply. For example, if you wanted the number of rows in each of your split data frames, you could do

mydf.counts <- sapply(mydf.split, function(x) nrow(x))
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263