mercredi 21 novembre 2018

loop ifelse for multiple data frames

I want to create a loop to repeat code lines in a list of datasets. Each dataset looks like:

gwas_1 
   ID   p
    1   0.0000005
    2   0.0123474
    ... 
gwas_2 
   ID   p
    1   0.0000055
    2   0.5854587
    ... 

So I would like to create a new column and check frequencies in a new column in each dataset.I did it before like this

  data=gwas_1
  data$p_threshold <- ifelse(data$p<0.001, 1, 0)
  table (data$p_threshold)
  data=gwas_2
  data$p_threshold <- ifelse(data$p<0.001, 1, 0)
  table (data$p_threshold) 

but realised that it might not very efficient. Could you please help me to create a loop as my loop doesn't work ("Error: $ operator is invalid for atomic vectors"):

 list=c("gwas_1, gwas_2, gwas_3")
 for (db in list){
 db$p_threshold <- ifelse(db$p<0.001, 1, 0)
 table (db$p_threshold)
 }

Aucun commentaire:

Enregistrer un commentaire