I have a dataframe df. I would like to replace all zeros with ones and all ones with zeros in the dataframe. Or in general, if I have to find some value (like one) and replace the value with something else, what's the best approach?
a b
1 1 0
2 0 1
3 1 0
4 0 0
A code golf would be to negate the dataset and use +
+(!df)
# a b
#1 0 1
#2 1 0
#3 0 1
#4 1 1
mydata=data.frame(a=c(1,0,1,0),b=c(0,1,0,0));
psych::reverse.code(c(-1,-1),mydata)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With