change all negative values in a column of a data frame to zero

r

Solution

You can use an `ifelse` command:

df$column <- ifelse(df$column < 0, 0, df$column)

or as @Jilber said in the comments:

df$column[df$column < 0] <- 0

or

within(df, column[column<0] <- 0)

Problem

In R, how can I change all negative values in a column of a data frame to zero? Is there a simple function that can be used together with `apply()` to do this job? Alternatively, how to write a loop to do it? Thank you very much!

Original source