jeudi 13 septembre 2018

R With an ifelse statement, how do I pull a value from another column when the condition is true?

There is probably a simple solution for this but I've been struggling all night to get this and I can't find the right search terms to find an answer, so I appreciate any help.

Essentially, I am filling in a new dataframe by looking for the name of the rows in a different dataframe2, and if that name is in dataframe2 then I want to pull the value in the second column of datagrame2...and if it is not, then I want to add a zero.

Here is the code I have .. but this is randomly pulling values if the name is in dataframe2.

UTR3s.totes.df <- UTR3s.df
for (i in 1:317) {
  targets.df <- subset(TC_merge,V14==consmiRNAs[i])[,c(18,5)]
  targets <- intersect(targets.df$ENSEMBL, UTR3s$Gene)
  targets.df <- subset(targets.df, targets.df$ENSEMBL %in% targets)
  sitenumber = data.frame(Gene = UTR3s$Gene)
  UTR3s.totes.df[, paste("target",i,sep="")] <- ifelse(sitenumber$Gene %in% 
targets.df$ENSEMBL, targets.df$V5, "0")
}

Everything is working except for the last line ... pulling the corresponding value from the dataframe2 (targets.df)

Aucun commentaire:

Enregistrer un commentaire