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
Reproducible data FTW.
Anyhow, here’s a guess:
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.
lapplytakes each element ofall_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 isdat, so that’s what the data.frame will be called each time that function is run (e.g. for each data.frame contained inall_list).Then
sapplyworks likelapplybut does some cute arranging if it can. So it takes a vector of functionsc(min,max,median)and assigns those to the first argument of the function in turn. That function’s first argument isfn, 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_listcalleddatand a single function calledfn, 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
sapplyworks its magic to return a matrix with the output in a tidy form. Thenlapplyincludes that matrix as a element of the list which it will ultimately give back to you.