[英]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.其中 1 是每年,2 是每周,3 是每月。
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.抱歉,如果这是一个非常简单的问题,我想我可以将 3 重新编码为 12,然后有一个公式将这些列相乘,然后将 2 重新编码为 52 并执行相同操作,只是想看看是否有人有更好的方法做事,因为实际上有多个这样的列,在我需要修复的时间段内具有不同的代码。
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)由reprex package (v2.0.0) 于 2021 年 4 月 13 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.