S4 Object data extraction in R
1
0
Entering edit mode
8 months ago
azh7018 • 0

I don't know if it is even possible. But I assume it should be. I will highly appreciate any help since I am very new.

counts is an S4 object I got from an online course I am doing for microbiome analysis.

 str(counts) Formal class 'dgCMatrix' [package "Matrix"] with 6 slots  
 ..@ i       : int [1:10483] 4 12 18 19 23 36 44 64 76 77 ...   ..@ p  
 : int [1:156] 0 59 144 229 289 368 416 462 552 625 ...   ..@ Dim     :
 int [1:2] 201 155   ..@ Dimnames:List of 2   .. ..$ : chr [1:201]
 "Methanobrevibacter_smithii" "Methanosphaera_stadtmanae"
 "Rothia_mucilaginosa" "Propionibacterium_freudenreichii" ...   .. ..$
 : chr [1:155] "PRISM.7122" "PRISM.7147" "PRISM.7150" "PRISM.7153" ... 
 ..@ x       : num [1:10483] 0.0001 0.0099 0.0077 0.0005 0.3367 ...  
 ..@ factors : list()

This is the structure and you can see the slots are of variable length. I would like to have a data frame with the Bacteria names as my rows which is under counts@Dimnames[[1]] and the counts@Dimnames[[2]] as my columns. And the values which are in counts@x into the respective locations which I can see if I simply print counts which looks something like this.

Methanobrevibacter_smithii       .      .       .      .      . .      .      .       .      .     
Methanosphaera_stadtmanae        .      .       .      .      . .      .      .       .      .     
Rothia_mucilaginosa              .      9.4e-06 .      .      . .       5e-04 1.0e-04  1e-04 .     
Propionibacterium_freudenreichii .      .       0.0018 .      . .      .      .       .      .     
Bifidobacterium_adolescentis      1e-04 .       0.0403  1e-04 .  1e-04 .      .       .       1e-04

Thanks in advance!

R S4 • 561 views
ADD COMMENT
1
Entering edit mode
8 months ago

You might be able to coerce it to a data.frame with as.data.frame(counts) or as.data.frame(as.matrix(counts)). You almost always want to avoid interacting with the slots directly in an S4 object, but instead find the appropriate generic or helper function (functions that work on the object). This is primarily because slots for an object can change with software versions (often without documentation), but generic or helper functions are generally stable (and are usually documented if there is a change).

ADD COMMENT

Login before adding your answer.

Traffic: 1459 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6