'How to replace values of 1 column with the same values but with a calc in r?

I'm working with a data frame with multiple columns and I want to replace the values for 1 column with the same value but, for example, divide by 2 and I don't want to create another column; I mean that I want to do it directly. For example, I have the following dataframe:

df <- data.frame(

val = c(2,4,6,8,10)

)

And I expect to replace all the values of the variable val with the same values, only with a calculation, like:

data.frame(

val = c(1,2,3,4,5)

)

Is there a way to do that?

Thanks for the help



Solution 1:[1]

mutate should work if writing the variable name again is not a problem for you.

data.frame(val = c(2,4,6,8,10)) %>% mutate(val = val / 2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 William Lima