I have a number of S4 objects with a variable number of slots and slotnames. The data in each slot is the same length. For example the S4 object peaks
:
> str(peaks)
Formal class 'MassPeaks' [package "MALDIquant"] with 4 slots
..@ snr : num [1:16] 37.81 9.18 8.65 4.66 53.22 ...
..@ mass : num [1:16] 307 317 325 337 347 ...
..@ intensity: num [1:16] 2255 547 516 278 3173 ...
..@ metaData : list()
I want to turn these S4 objects into dataframes. For example the dataframe df.peaks
:
> str(df.peaks)
'data.frame': 16 obs. of 3 variables:
$ snr : num 37.81 9.18 8.65 4.66 53.22 ...
$ mass : num 307 317 325 337 347 ...
$ intensity: num 2255 547 516 278 3173 ...
Is there a general way to do this? For this example I can do
> df.peaks = data.frame(snr=peaks@snr, mass=peaks@mass, intensity=peaks@intensity)
but that requires hardcoding the slotnames of the S4 object. How do I do it without hardcoding the slotnames? I could do it by getting s4.names = slotNames(peaks)
, but in this case I don't know how to use s4.names
to access the slots of peaks
.