[adegenet-forum] extracting genefreq $tab from an indexed list

Roman Lustrik roman.lustrik at biolitika.si
Mon Mar 30 08:08:22 CEST 2015


S4 objects are different to the classical S3 object like data.frames, lists and other "basic" objects. One of their peculiarities is that they're accessed through "@" operator. In truth, user is not meant to access the slots directly - the developer should provide methods to access all the slots that she or he deems appropriate for user to access. No method could mean either it hasn't been implemented yet, or is not implemented by design (Thibaut will have more to say about this). 

And now crux of the matter. Your first two examples work because lists can be accessed through various operators. This is often done via `lapply(X = x, FUN = "[[", "element_name")`. In your case, you can try creating an anonymous function that accesses the slot. 



library(adegenet) 
data(nancycats) 
x <- list(nancycats) 
lapply(x, FUN = function(x) x$tab) 







Cheers, 

Roman 






---- 
In god we trust, all others bring data. 

----- Original Message -----

From: "Vikram Chhatre" <crypticlineage at gmail.com> 
To: adegenet-forum at lists.r-forge.r-project.org 
Sent: Monday, March 30, 2015 4:25:16 AM 
Subject: [adegenet-forum] extracting genefreq $tab from an indexed list 

I am working with hundreds of genpop objects indexed in a list. Using lapply and makefreq functions, population gene frequencies were stored in individual objects (1 per data set). 

Here is an example with just three objects: 

>summary(mygenpop) 
Length Class Mode 
data1.str 1 genpop S4 
data2.str 1 genpop S4 
data3.str 1 genpop S4 

>mygenfreq <- lapply(mygenpop, function(x) makefreq(x, truenames=TRUE)) 

>summary(mygenfreq) 
Length Class Mode 
data1.str 3 -none- list 
data2.str 3 -none- list 
data3.str 3 -none- list 

>summary(mygenfreq[[1]]$tab) 
> str(mygenfreq[[1]]) 
$ tab : num [1:30, 1:1974] 0.6 0.5 0.325 0.675 0.6 0.5 0.5 0.375 0.55 0.475 ... 
$ nobs: num [1:30, 1:1000] 40 40 40 40 40 40 40 40 40 40 ... 

Next job is to work with the $tab matrix, but I am not sure how to access it from all objects in one command. 

>mygenfreqT <- lapply(mygenfreq[[1:3]]$tab, function(x) t(x)) 

This throws an error. The syntax seems to be wrong, but I am not sure how to fix this. Thanks for any help. 

Vikram 



_______________________________________________ 
adegenet-forum mailing list 
adegenet-forum at lists.r-forge.r-project.org 
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/adegenet-forum 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/adegenet-forum/attachments/20150330/2268f83e/attachment.html>


More information about the adegenet-forum mailing list