1

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!

2 Answers 2

2

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)))
Sign up to request clarification or add additional context in comments.

Comments

2

You could also do:

dt$wage_base <- dt$wage_base * 1.2^(dt$year == 2010)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.