I have a dataframe consisting of four columns. The third is a factor variable, with levels A and B, and the fourth variable is numeric. I want to flip the sign of the numeric variable if the corresponding factorlevel is B. So, given a row x of my dataframe, I want to do this:
negate <- function(x) {
if(x[3] == B) {
x[4] <- -x[4]
}
}
However, when I try
apply(dataframe,1,negate)
I get the following error:
Error in -x[4] : invalid argument to unary operator
How do I do what I want to do? Thanks in advance.