Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R lapply ifelse with multiple statements on list of dataframes

I have a list (fc.list) of dataframes ("t1.fc.df", "t2.fc.df") that looks like this:

fc.list$t1.fc.df
ID   log2.FC    qval
1    5.22161    0
2    4.34383    0
3    3.764772   0.86250849
4   -3.095648   0.9412494
5   -3.489743   0.904717
6   -3.648665   0.9412494

fc.list$t2.fc.df
ID   log2.FC    qval
1    6.287703   0.034547415 
2    5.751197   0.007923771
3    5.093789   0.352390406
4   -5.337459   0.007400576
5   -5.760159   0.000000000
6   -6.793630   0.000000000

I need to create a variable in my dataframes called $test which says if log2.FC is > 1 and qval is < 0.05 then write "positive", else, if log2.FC is < -1 and qval is < 0.05 write "negative", else write "NS".

I write these lines of code with lapply ifelse,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fc.list <- lapply(fc.list, function(x){
  x$test <- ifelse(unlist(x[1]) >= 1 &&
                       unlist(x[2]) <= 0.05, "positive",
                      ifelse(unlist(x[1]) <= -1 &&
                               unlist(x[2]) <= 0.05, "negative", "NS"))
  return(x)
})

but I get only "NS" Can anyone figure it out where is the problem?
Thanks.

>Solution :

You can try this

lapply(fc.list, function(x) 
        cbind(x, type = ifelse(x$qval<0.05 & x$log2.FC>1, "positive", 
                        ifelse(x$qval<0.05 & x$log2.FC< -1, "negative", NA))))
$t1.fc.df
  ID   log2.FC      qval     type
1  1  5.221610 0.0000000 positive
2  2  4.343830 0.0000000 positive
3  3  3.764772 0.8625085     <NA>
4  4 -3.095648 0.9412494     <NA>
5  5 -3.489743 0.9047170     <NA>
6  6 -3.648665 0.9412494     <NA>

$t2.fc.df
  ID   log2.FC        qval     type
1  1  6.287703 0.034547415 positive
2  2  5.751197 0.007923771 positive
3  3  5.093789 0.352390406     <NA>
4  4 -5.337459 0.007400576 negative
5  5 -5.760159 0.000000000 negative
6  6 -6.793630 0.000000000 negative

Or a tidyverse

library(tidyverse)
fc.list %>% 
  map(~mutate(., type=case_when(qval<0.05 & log2.FC>1 ~ "positive",
                                qval<0.05 & log2.FC< -1 ~ "negative",
                                TRUE ~ NA_character_)))

by adding %>% map(count, type) you get the numbers of deregulated features:

$t1.fc.df
      type n
1 positive 2
2     <NA> 4

$t2.fc.df
      type n
1 negative 3
2 positive 2
3     <NA> 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading