mardi 2 février 2021

How to correctly recode the variable in data frame using if ...else command in r language? [duplicate]

The dataset df has 2 variables- the 1st column is self-isolation (si) and the 2nd column is the degree of agony (agony). Both of them have missing values.

Now, I want to recode the variable "agony" with the following rules,

  1. when the si==1, agony=="NR"

  2. no any other changes to the data except for rule1 above.

structure(list(si = c("0", "1", NA, "0", "0", NA, "0", "1", NA
), agony = c("6", NA, NA, "2", "3", NA, "12", NA, "2")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))->df

The expected result is as follows,

si          agony
0             6
1             NR
NA            NA
0              2
0              3
NA             NA
0              12
1              NR
NA              2

Aucun commentaire:

Enregistrer un commentaire