4
I have the following date. ::
df <- data.frame(Qtd=c(100,200,300,400,500),
op=c('V','C','C','V','V'), stringsAsFactors=F)
How I can turn the values in the Qtd column to negative if the op column value is V ??
I tried this command but it was wrong :
Teste <- within(df, Qtd[op == 'V'] <- -Qtd[])
Warning message:
In Qtd[op == "V"] <- -Qtd[] :
number of items to replace is not a multiple of replacement length
I think the use of
which
is redundant, there is no reason to detect the condition indices if the TRUE/FALSE values themselves are sufficient to subseting and change only the V values. The second option can simply bedf$Qtd[df$op == "V"] <- -df$Qtd[df$op == "V"]
– Molx