mercredi 2 décembre 2020

Nested for loop in R for going through each row of each column of a data frame

I am trying this nested R loop but the current script writes always no. Basically I want it to go over each row of the first column in "dta", check if that string is present in the rows of "samples" and write yes or no. The issue is that I do not know how to code it to go over each row of each column of "samples".

How can I specify to loop over each column j in "samples"? I know I am missing something in dta[i,]<-'Yes' and dta[i,]<-'No'

for (j in colnames(samples)){
  for (i in 1:nrow(dta)){
    if(dta[i,1]%in%samples[j]){
      dta[i,]<-'Yes'
    }else(
      dta[i,]<-'No'
    )
  }
}

Thanks

Aucun commentaire:

Enregistrer un commentaire