mardi 30 mars 2021

How can I change the values of one column using the values of another column or row in R? [duplicate]

I want to change the category value to "z" when the id = 3,4,9 or do the same thing using row number. How can I do it? Something like if rownumber = 3,4,9 then change to category = "z" or if id = 3,4,9 then change the category to "z"

id <- [1:10]
category <- rep(c("x","y"), times = 5)
df <- data.frame(id, category)
print (df)

   id category
1  1  x
2  2  y
3  3  x
4  4  y
5  5  x
6  6  y
7  7  x
8  8  y
9  9  x
10 10 y
I want to get this output
   id category
1  1  x
2  2  y
3  3  z
4  4  z
5  5  x
6  6  y
7  7  x
8  8  y
9  9  z
10 10 y

Aucun commentaire:

Enregistrer un commentaire