Reputation: 65
So I have a data frame with 2 columns. In the first column (year) I have data from multiple years. In the second column (wage_base) I have data regarding the base wage.
I want to multiply all the rows of the data frame belonging to the year 2010, for example, by the value of 1.2 and want to change the data frame permanently with these new values. I tried this so far:
dt$wage_base <- dt$wage_base * 1.2
I am just missing the part where I state that I only want to multiply 1.2 by the values of the year 2010.
Thank you for your help!
Upvotes: 1
Views: 1301
Reputation: 79338
You could also do:
dt$wage_base <- dt$wage_base * 1.2^(dt$year == 2010)
Upvotes: 2
Reputation: 887891
We can create a logical condition
i1 <- dt$year == 2010
dt$wage_base[i1] <- dt$wage_base[i1] * 1.2
Or with data.table
library(data.table)
setDT(dt)[, wage_base := as.numeric(wage_base)
][year == 2010, wage_base := wage_base * 1.2]
Or with dplyr
library(dplyr)
dt %>%
mutate(wage_base = case_when(year == 2010 ~ wage_base * 1.2,
TRUE ~ as.numeric(wage_base)))
Upvotes: 2