Home > database >  write for cycle with ifelse in R
write for cycle with ifelse in R

Time:10-27

I need a help to write a for cycle for my script. I was able to write one by one all the scripts, but I have to work with a large amount of data. Thank you.

head(data)
# A tibble: 6 × 7
  genes    Ctrl                  cKO                 `Fold Change`        `p-value`               `adjusted p-value`      Cluster
  <chr>    <chr>                 <chr>               <chr>                <chr>                   <chr>                   <chr>  
1 gene1   0.187898345240537     2.3628505591503899  2.53172293340142     0                       0                       C1     
2 gene2   2.6417765969217299    0.60390515723054905 -1.4757897620357101  9.3204880139383496E-216 4.7357399598820701E-212 C2     
3 gene3   5.1104039814855402E-2 2.03306469601706    3.6834360849252401   5.5256116322696504E-211 1.8717088469041399E-207 C3     
4 gene4   3.1185212118408599E-2 1.33522156824269    3.7569085138918998   9.1578480277593798E-122 2.32655129145227E-118   C4     
5 gene5   0.729080925630612     0.36812561487082501 -0.68336051026139    9.6266493451706304E-40  1.2228251330703001E-36  C4     
6 gene6   0.90455640965217299   0.523241638673281   -0.54740128685771505 9.7760757384136695E-35  8.2787068044799699E-32  C5  

head(mytab)
# A tibble: 6 × 5
  genes  `Fold Change` avg_log2FC  comparison
  <chr>          <dbl>      <dbl>    <lgl>     
1 gene1        -0.524     -0.254     TRUE      
2 gene2        -0.509     -0.244     TRUE      
3 gene3        -0.550     -0.313     TRUE      
4 gene4        -0.642     -0.331     TRUE      
5 gene4        -0.633     -0.331     TRUE      
6 gene4        -0.507     -0.331     TRUE


 tab1 = data[data$Cluster == "C1",]
 tab2 = data[data$Cluster == "C2",]
 tab3 = data[data$Cluster == "C3",]
 tab4 = data[data$Cluster == "C4",]
 tab5 = data[data$Cluster == "C5",]
 tab6 = data[data$Cluster == "C6",]


 T1= as.data.frame(ifelse(unique(mytab$genes) %in% tab1$genes, 1, 0))
 T2= as.data.frame(ifelse(unique(mytab$genes) %in% tab2$genes, 1, 0))
 T3= as.data.frame(ifelse(unique(mytab$genes) %in% tab3$genes, 1, 0))
 T4= as.data.frame(ifelse(unique(mytab$genes) %in% tab4$genes, 1, 0))
 T5= as.data.frame(ifelse(unique(mytab$genes) %in% tab5$genes, 1, 0))
 T6= as.data.frame(ifelse(unique(mytab$genes) %in% tab6$genes, 1, 0))

my output should be:

          C 1 C 2 C 3 C 4 C 5 C 6 C 7 C 8 C 9 C 10 C 11 C 12 C 13 C 14 C 15 C 16 C 17 C 18
gene1      0   0   0   0   0   0   0   0   0    1    0    0    0    1    0    0    0    0
gene2      0   0   0   0   1   1   0   0   0    0    0    0    0    0    1    0    0    0
gene3      0   0   0   0   0   0   0   0   1    0    0    0    0    0    0    0    0    0
gene4      0   1   0   0   0   0   0   1   0    0    0    0    0    0    0    0    0    1
gene5      0   0   0   0   0   0   0   0   0    0    1    0    0    0    0    0    0    0
gene6      0   0   0   0   0   0   0   0   1    0    0    0    0    0    0    0    0    0

CodePudding user response:

You should add a small reproducible example and a piece of expected output for we to better understand your problem and give a correct answer to you. Based on your attemp this code might help

tmp <- lapply(split(data, data$Cluster), function(x){
  (unique(mytab$genes) %in% x$genes)*1
})
names(tmp) <- paste0("T", 1:6)
list2env(tmp, envir = .GlobalEnv)
  • Related