I would like to multiply my r dataframe with minus 1, in order to reverse the signs of all values (turn + to - and vice versa):
This does not work:
df_neg <- df*(-1)
Is there another way to do this?
I would like to multiply my r dataframe with minus 1, in order to reverse the signs of all values (turn + to - and vice versa):
This does not work:
df_neg <- df*(-1)
Is there another way to do this?
Here'a a tidyverse way to alter only the numeric columns.
library(dplyr)
df_neg <- df %>%
mutate_if(is.numeric, funs(. * -1))
Assuming your data frame is all numeric, the code you posted should work. I'm going to assume you have some non-numeric values we need to work around
# make a fresh copy
df_neg <- df
# now only apply this to the numeric values
df_neg[sapply(df_neg, is.numeric)] <- df_neg[sapply(df_neg, is.numeric)] * -1