简体   繁体   中英

Income at different time periods (year, month, week)- I want to standardise the data

I have a dataset that looks a bit like this:

Income Income period
1500 3
400 2
30000 1

Where 1 is yearly, 2 is weekly, and 3 is monthly.

I want to create a column that will show the income yearly for all rows so that I can compare them more easily.

Apologies if this is a very simple question, I guess I could recode 3 to be 12 and then have a formula that multiplies these columns together and then recode 2 to be 52 and do the same, just wanted to see if anyone has a better way of doing things as there are actually multiple columns like this with different codes for time periods that I need to fix.

  library(dplyr)

  df %>% 
    mutate(income_yr = case_when(period == 3 ~ income * 12,
                                 period == 2 ~ income * 52,
                                 TRUE ~ income))
#>   income period income_yr
#> 1   1500      3     18000
#> 2    400      2     20800
#> 3  30000      1     30000

data

  df <- data.frame(income = c(1500, 400, 30000),
                   period = c(3, 2, 1))

Created on 2021-04-13 by the reprex package (v2.0.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM