Lin Ma Lin Ma - 4 months ago 9
R Question

smarter way to re-assign values for a column in dataframe in R

Suppose have a data frame

df
and column
foo
is float. I want to assign
foo
to be 1, if the value of
foo
is more than one. Wondering if any efficient way to implement in R? If there is a one line implementation, it will be great, similar to lambda expression in Python. Thanks.

Answer

Try:

df$foo[df$foo > 1] <- 1
Comments