[英]Error in mutate_impl(.data, dots) : Evaluation error: Only year, quarter, month, week, and day periods are allowed for an index of class Date
[英]Income at different time periods (year, month, week)- I want to standardise the data
我有一個看起來有點像這樣的數據集:
收入 | 收入期 |
---|---|
1500 | 3 |
400 | 2 |
30000 | 1 |
其中 1 是每年,2 是每周,3 是每月。
我想創建一個列來顯示所有行的年度收入,以便我可以更輕松地比較它們。
抱歉,如果這是一個非常簡單的問題,我想我可以將 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
數據
df <- data.frame(income = c(1500, 400, 30000),
period = c(3, 2, 1))
由reprex package (v2.0.0) 於 2021 年 4 月 13 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.