'How can I apply a calculation to a dataframe when a value is not equal to a specified string?
Say I have the following table and only want to sum when the column does not contain "Fred". If the value does contain "Fred" I want to sum and multiply by 2. How can I achieve this preferably using dplyr?
Name | x | Expected Value |
Jack | 15 | 17 |
Sally | 3 | 3 |
Fred | 10 | 20 |
Jack | 2 | 17 |
Obviously this is not a serious table, but in essence, I just want to know how to apply a different basic calculation for a specified unique value in a large dataset.
Solution 1:[1]
in Base R:
transform(df, expect = ave((1 + (Name == 'Fred')) * x, Name, FUN = sum))
Name x expect
1 Jack 15 17
2 Sally 3 3
3 Fred 10 20
4 Jack 2 17
Consider using tidyverse:
library(tidyverse)
df %>%
group_by(Name) %>%
mutate(expect = sum((1 + (Name == 'Fred')) * x))
Name x expect
<chr> <int> <dbl>
1 Jack 15 17
2 Sally 3 3
3 Fred 10 20
4 Jack 2 17
Solution 2:[2]
this is a a very simple way to solve your situation:
sample_table<- data.table(
Name = c('Jack','Sally','Fred','Jack'),
x = c(15,3,10,2),
exp = c(17,3,20,17))
sample_table[Name%in%"Fred",end_value:=(x+exp)*2]
sample_table[!(Name%in%"Fred"),end_value:=(x+exp)]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | onyambu |
| Solution 2 | leonardo zamarchi |
