vendredi 17 février 2017

Iterate over a data frame to randomly select samples with conditionals

I would like to iterate over samples to select 12 rows randomly that match criteria in Vector.

>samples        
     ID          MP          BP
     FFP-010     -1          Basal
     FFP-011     -0.8        ERBB
     FFP-012      0.2        Luminal
     ....

In my function, the string below is meant to specify the conditions (MP,BP) for the 12 rows of samples I want to be returned. I filtered samples to contain the BP type values and the value of MP I want in each loop.

>Vector
     ("-1;Basal","-0.81;ERBB","-0.68;Basal","-0.53;Luminal","-0.33;Luminal","-0.18;Luminal","0.1;Luminal","0.18;Luminal","0.23;Luminal","0.49;Luminal","0.56;Luminal","0.71;Luminal"")

The function is supposed to sample without replacement until it finishes the 12 conditions in Vector.

>Sample_Selector <- function(x){
      x <- as.data.frame(Vector)
      colnames(x) <- "Nope"
      Sample_Criteria <- separate(x, Nope, sep=";",c("MP", "BP"), remove=T, convert=T)

      Candidates <- samples %>%
            filter(BP==Sample_Criteria$BP,
            MP <= Sample_Criteria$MP + range & MP >= Sample_Criteria$MP - range)

      THE_SAMPLE <- Candidates[sample(rownames(Candidates), 1, replace=F),]

      return(THE_SAMPLE)}

> SampleList <- map_df(Vector, Sample_Selector)

My code keeps returning 12 samples but they are random without my conditions. I don't think Sample_Selector correctly iterating over Vector and removing the conditions each loop.

For example, I keep getting lists like this:

>SampleList        
     ID          MP          BP
     FFP-015     -1        Basal
     FFP-033     -1        Basal
     FFP-009     -1        Basal
     ....




Aucun commentaire:

Enregistrer un commentaire