jeudi 7 septembre 2017

How to replace rows based on conditions?

This is a very simple request, but I can't wrap my head around it. I've created a flag variable (0,1), and I would like to replace the values of one column with another conditionally on if flag==1.

Here is a reproducible example and my incorrect code:

if_else(q46$flag==1,q46$name==q46$positions,q46$name==q46$name)


q46 <- data.frame(flag=sample(0:1,100,replace=T),
       name=sample(c('Bob','Susan','Susan','Amy','Elliot'),100, replace=T),
      positions=sample(c('President','Agronomist','Preacher'),100,replace=T))

Aucun commentaire:

Enregistrer un commentaire