[R] sample from list

Jean V Adams
Wed, 07 Nov 2012 21:12:29 -0000

Ben,

Can you provide a small example data set for
        inds
so that we can run the code you have supplied?
It's difficult for me to follow what you've got and where you're
trying to
go.

Jean



"Benjamin Ward (ENV)" <B.Ward@uea.ac.uk> wrote on 11/06/2012
03:29:52 PM:
>
> Hi all,
>
> I have a list of genes present in 500 individuals, the individuals
> are the elements:
> Genes <- lapply(1:nrow(inds),function(x) sample(1:10000,inds
> $No_of_Genes,replace=TRUE))
>
> (This was later written to a dataframe as well as kept as the list
> object: inds2 <- data.frame(inds,Genes=I(Genes)))
>
> I also have a vector of  how many of those genes are expressed in
> the individuals, this can also kept as a vector object or written to
> a data frame:
>
> inds2$No_Expressed_Genes <- round(as.numeric(lapply(1:nrow
> (inds2),function(x) runif(1, min=10, max=50))))
>
> I want to create another list which consists of each individuals
> expressed genes - essentially a subset of the total genes the
> individuals have in the "Genes" list, by sampling from the Genes
> list for each individual, the number of genes (values)in the
> Num_Expressed_Genes vector. i.e. if Num_Expressed_Genes = 3 then
> sample 3 values from the element in the Genes list. I can't quite
> figure it out though. So far I have the following:
>
> #Defines The number of expressed genes for each individual in my data
frame.
> Num_Expressed_Genes <- round(as.numeric(lapply(1:nrow
> (inds2),function(x) runif(1, min=10, max=50))))
>
>
> #My attempts to apply the sample function to every element
> (individual organism) of the "Genes" list , to subset the genes
expressed.
> Expressed_Genes <- lapply(1:nrow(inds),function(x) sample
> (Genes,Num_Expressed_Genes, replace=FALSE))
> Expressed_Genes <- lapply(Genes,function(x) sample
> (Genes,Num_Expressed_Genes, replace=FALSE))
>
> So far though I'm getting results like this:
>
> [[49]]
> [[49]][[1]]
>   [1] 3540   27 5344 7278 9758 8077 ...............................
[217]
>
>
> [[49]][[2]]
>   [1]  740 3362 8588 8574 4371 1447 .............................. [340]
>
>
> When what I need is more:
>
> [[49]]
> [1] 6070 1106 6275
> In a case where Num_Expressed_Genes = 3 and the values are taken
> from the much larger set of values for element (individual) 49 in my
> Genes list.
>
> I'm not sure what I'm doing wrong but it seems what is happening is
> instead of picking out a few values according to the
> Num_Expressed_Genes vector - as an example say 3 again, It's drawing
> a large number of values, if not all of them, from elements in the
> list, 3 times.
>
> Any help is greatly appreciated,
> I've thought of using loops to achieve the same task, but I'm
trying
> to get my individual/genes/expressed genes data.frame set up for my
> individual based model and get it running using vectors and as
> little loops as possible.
>
> Thanks,
> Ben.

	[[alternative HTML version deleted]]