oikonomiyaki
oikonomiyaki

Reputation: 7951

Change column values on specific rows only R

I have a dataframe in R, and I need to clean a specific column but not on all rows. For example, for rows with

df$score > 100

I want to divide it by 10 and replace the original value.

How do I do this in R?

Upvotes: 0

Views: 3524

Answers (2)

akrun
akrun

Reputation: 886938

Using data.table, we can assign in place for those rows that match the condition set in 'i'.

library(data.table)
setDT(df)[score>100, score := score/10]

Upvotes: 2

Hardik Gupta
Hardik Gupta

Reputation: 4790

Using mtcars as example, you can do it like this:

mtcars$mpg <- ifelse(mtcars$mpg > 100, mtcars$mpg/10, mtcars$mpg)

Upvotes: 4

Related Questions