Reputation: 87
I have a list of 27 data frames with different numbers of rows in each frame but the same number of columns (22). I need the min, max, and median of each column put into a vector for each frame, the result should look something like this:
frame1 = (c1min, c1max, c1median, c2min, c2max, c2median ... c22min, c22max, c22median)
frame2 = (c1min, c1max, c1median, c2min, c2max, c2median ... c22min, c22max, c22median)
...
frame27 = (c1min, c1max, c1median, c2min, c2max, c2median ... c22min, c22max, c22median)
I've generated the min of each column doing this:
> all_min = lapply(all_list, function(x){apply(x,2,min)})
Where 'all_list' is my list and 'all_min' is the vector for all the column mins in the list. I'm unsure as to how to get an numbered list of vectors (like in the example above), and how to get them properly formatted. Any ideas?
EDIT: Here's an example of one of the frames:
lx ly lz ...
1 -0.039256 -0.078644 -0.032388
2 -0.036041 -0.074536 -0.033906
3 -0.033898 -0.071544 -0.033906
Upvotes: 1
Views: 1011
Reputation: 72731
Reproducible data FTW.
Anyhow, here's a guess:
lapply( all_list, function(dat) {
sapply( c(min,max,median), function(fn) apply(dat,2,fn) )
})
Hard to test with no sample data though.
This works by passing the functions as arguments to sapply. One of many ways in which "everything is an object" to R.
lapply
takes each element of all_list
(in this case, a data.frame) and passes it as the first argument to the function it's given. That function's first argument is dat
, so that's what the data.frame will be called each time that function is run (e.g. for each data.frame contained in all_list
).
Then sapply
works like lapply
but does some cute arranging if it can. So it takes a vector of functions c(min,max,median)
and assigns those to the first argument of the function in turn. That function's first argument is fn
, so that's what each function will be called every time it is run. Cool.
So now we have, for a single data.frame in all_list
called dat
and a single function called fn
, a simple function that does what you'd originally set out--apply's that summary function to each column of the data.frame.
The final step is that sapply
works its magic to return a matrix with the output in a tidy form. Then lapply
includes that matrix as a element of the list which it will ultimately give back to you.
Upvotes: 3