jeudi 20 août 2020

Changing value of one column based on condition of another with a loop R

I have a data frame that looks something like this, but it has many paired columns following the format "Name", "F_Name".

    data.frame("Site" = c("X", "Y", "Z"), "Temp" = c(2, 3, 4), "F_Temp" = c(1, 5, -3), "Salinity" = c(5, 6, 7), "F_Salinity" = c(6, 1, -3))

Whenever "F_Name" equals 1 or -3, I want to change the value of "Name" to NA. For example, in the rows where F_Temp equals -3 or 1, I want to change the value of Temp to be NA.

I would like to do this using a loop, as there are many paired columns and I don't want to have to type them all out. What is the best way to go about this?

Aucun commentaire:

Enregistrer un commentaire