Reputation: 3
I am a beginner using R so apologies if this is a stupid question. I have a data frame that has a column containing 7000 values. I want to calculate the negative inverse (-1/value) for all values <1 in that column while leaving all values >= 1 unchanged.
What is the easiest way of doing this?
Upvotes: 0
Views: 262
Reputation: 18662
You could also also subset and replace your vector where your condition is TRUE
:
v[v < 1] <- -1 / v[v < 1]
Upvotes: 0
Reputation: 102241
Perhaps you can try replace
like below
replace(v, v<1,-1/v[v<1])
or a more mathy way
(1 - 2 * (v < 1)) * v^(1 - 2 * (v < 1))
Upvotes: 1
Reputation: 8582
say your data is called df
and has a column called values
you could do:
ifelse(df$values < 1,
-1 / df$values, #negative inverse of all <1
df$values) #keep others the same
Upvotes: 1