[英]Alternative for giant nested ifelse statement
I have a data frame that looks like the following:我有一个如下所示的数据框:
Period No. Frequency
1 Month
2 Month
3 Month
3 Quarter
6 Quarter
9 Quarter
1 YTD
2 YTD
3 YTD
I want to add on a column called "Period" whose values are determined by what is in the Period No. AND Frequency columns.我想添加一个名为“Period”的列,其值由 Period No. AND Frequency 列中的内容确定。 So:所以:
Period No. Frequency Period
1 Month 1
2 Month 2
3 Month 3
3 Quarter Q1
6 Quarter Q2
9 Quarter Q3
1 YTD YTD-Jan
2 YTD YTD-Feb
3 YTD YTD-Mar
Right now, I'm using nested if statements to do this.现在,我正在使用嵌套的 if 语句来执行此操作。 For example:例如:
data$Period <-
ifelse(
(data$`Period No.` == '3') & (data$Frequency == 'Q1'), 'Q1',
ifelse(
(data$`Period No.` == '6') & (data$Frequency == 'Q2'), 'Q2',
ifelse(
(data$`Period No.` == '9') & (data$Frequency == 'Q3'), 'Q3', 'ERROR'
)
)
)
If I were to do this for every month for each iteration of Frequency, I would have 30 nested ifelse statements.如果我每个月对频率的每次迭代都执行此操作,我将有 30 个嵌套的 ifelse 语句。 I'm wondering if there's a more concise method to do what I'm trying to achieve?我想知道是否有更简洁的方法来做我想要实现的目标?
You only really need to nest ifelse
statements if the conditions themselves are nested.如果条件本身是嵌套的,您只需要嵌套ifelse
语句。 In this case, the conditions are mutually exclusive, so you can assign to each set individually.在这种情况下,条件是互斥的,因此您可以单独分配给每个集合。 We can also be a little clever about creating your results with paste
and some math rather than listing all the possibilities:我们也可以稍微巧妙地使用paste
和一些数学来创建您的结果,而不是列出所有可能性:
df$Period[df$Frequency == "Month"] = as.character(df$Period_No[df$Frequency == "Month"])
df$Period[df$Frequency == "Quarter"] = paste0("Q", (df$Period_No[df$Frequency == "Quarter"] - 1) %/% 3 + 1)
df$Period[df$Frequency == "YTD"] = paste0("YTD-", month.abb[df$Period_No[df$Frequency == "YTD"]])
df
# Period_No Frequency Period
# 1 1 Month 1
# 2 2 Month 2
# 3 3 Month 3
# 4 3 Quarter Q1
# 5 6 Quarter Q2
# 6 9 Quarter Q3
# 7 1 YTD YTD-Jan
# 8 2 YTD YTD-Feb
# 9 3 YTD YTD-Mar
If you like dplyr
, I'd recommend the case_when
function:如果你喜欢dplyr
,我推荐case_when
function:
df %>% mutate(Period = case_when(
Frequency == "Month" ~ as.character(Frequency),
Frequency == "Quarter" ~ paste0("Q", (Period_No - 1) %/% 3 + 1),
Frequency == "YTD" ~ paste0("YTD-", month.abb[Period_No])
))
Using this sample data:使用此示例数据:
df = read.table(text = "Period_No Frequency
1 Month
2 Month
3 Month
3 Quarter
6 Quarter
9 Quarter
1 YTD
2 YTD
3 YTD", header = T)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.