Katharina
Katharina

Reputation: 3

Replace values in certain rows based on value in different row

I have run into the following problem: due to a programming mistake, I had to change my rating scale from -4 to +4 to a scale from 0 to 9 temporarily. This results in 8 values in my data frame that I want to recode based on the ID that is defined in a different column.

id selfassessment
202 5
203 5
204 7
205 8
206 9
207 7
208 6

So for only those 8 rows in 'selfassessment' I want to change the values with 5 to 0, 6 to 1, 7 to 2, 8 to 3 and 9 to 4 without changing anything for the rest of the column. Can someone help me with this? Thank you!

Upvotes: 0

Views: 276

Answers (2)

You can use this code:

df$selfassessment <- ifelse(df$selfassessment<5, 0, df$selfassessment-5)

Upvotes: 1

ags29
ags29

Reputation: 2696

You can try:

df$selfassessment <- df$selfassessment - ifelse(df$selfassessment>=5, 5, 0)

Upvotes: 0

Related Questions