我有一个简单的df,它有列(segment、study_name、Effective_Overall_TopboxMean和其他一些基于数字的字段)。
Dput在这里:
structure(list(segment = c("Developers", "lowcode"), study_name = c("DeleteMe",
"DeleteMe"), Effective_Overall_TopboxMean = c(0.96875, 0.5),
Effective_Effective_TopboxMean = c(1, 0.571428571428571),
Effective_Useful_TopboxMean = c(1,0.571428571428571),Effective_Performant_TopboxMean = c(0.916666666666667,
0.380952380952381), Efficient_Overall_TopboxMean = c(0.765625,
0.357142857142857), Efficient_Efficient_TopboxMean = c(0.78125,
0.357142857142857), Efficient_Ease_TopboxMean = c(0.796875,
0.321428571428571), Efficient_Learn_TopboxMean = c(0.6875,
0.428571428571429), Empowered_Overall_TopboxMean = c(0.8375,
0.414285714285714), Empowered_Satisfaction_TopboxMean = c(0.84375,
0.321428571428571), Empowered_Empowered_TopboxMean = c(0.75,
0.523809523809524), Empowered_Enjoyable_TopboxMean = c(0.916666666666667,
0.428571428571429), UXQ_Overall_TopboxMean = c(0.827205882352941,
0.411764705882353), USE_Mean = c(0.816825396825397, 0.816825396825397
), USE_Usefulness_Mean = c(0.851190476190476, 0.851190476190476
), USE_Ease_Mean = c(0.774891774891775, 0.774891774891775
), USE_Learning_Mean = c(0.814285714285714, 0.814285714285714
), USE_Satisfaction_Mean = c(0.844897959183673, 0.844897959183673
), USE_SNOW_Mean = c(0.811904761904762, 0.811904761904762
), UXQ_Overall_Mean = c(0.81624649859944, 0.81624649859944
), run_date = structure(c(18865, 18865), class = "Date"),
TaskCompletionRate = c(0.561904761904762, 0.561904761904762
), WeightedScore = c(0, 0), ExperienceGrade = c("F Unacceptable",
"F Unacceptable")), row.names = c(NA, -2L), groups = structure(list(
segment = c("Developers", "lowcode"), .rows = structure(list(
1L, 2L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))'我的使用if / else语句的突变可以正常工作,但今天它似乎坏了,我无法解决错误消息:“
'Error: Problem with `mutate()` column `WeightedEffective`.
ℹ `WeightedEffective = if (...) NULL`.
ℹ `WeightedEffective` must be size 1, not 2.
ℹ The error occurred in group 1: segment = "Developers".
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning messages:
1: Problem with `mutate()` column `WeightedEffective`.
ℹ `WeightedEffective = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
ℹ The warning occurred in group 1: segment = "Developers".
2: Problem with `mutate()` column `WeightedEffective`.
ℹ `WeightedEffective = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
ℹ The warning occurred in group 1: segment = "Developers".
3: Problem with `mutate()` column `WeightedEffective`.
ℹ `WeightedEffective = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
ℹ The warning occurred in group 1: segment = "Developers".
4: Problem with `mutate()` column `WeightedEffective`.
ℹ `WeightedEffective = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
ℹ The warning occurred in group 1: segment = "Developers". '这是我的脚本:
UXQ_Boxes <- mutate(UXQ_Boxes,WeightedEffective =
if (UXQ_Boxes$TaskCompletionRate >.900){
(UXQ_Boxes$Effective_Overall_TopboxMean)*1
} else {
if (UXQ_Boxes$TaskCompletionRate >.790){
((UXQ_Boxes$Effective_Overall_TopboxMean)*.9)*100
} else {
if (UXQ_Boxes$TaskCompletionRate >.650){
((UXQ_Boxes$Effective_Overall_TopboxMean)*.8)*100
} else {
if (UXQ_Boxes$TaskCompletionRate >.500){
((UXQ_Boxes$Effective_Overall_TopboxMean)*.6)*100
} else {
if (UXQ_Boxes$TaskCompletionRate >.250){
((UXQ_Boxes$Effective_Overall_TopboxMean)*.4)*100
} else {
if (UXQ_Boxes$TaskCompletionRate <.260){
((UXQ_Boxes$Effective_Overall_TopboxMean)*.2)*100
}}}}}}*6)'任何关于如何解决这个问题的建议都将不胜感激。
发布于 2021-08-26 01:29:36
在使用mutate时使用if_else而不是if
UXQ_Boxes2 <- UXQ_Boxes %>%
mutate(UWeightedEffective = 6*if_else(TaskCompletionRate > .900, Effective_Overall_TopboxMean*1,
if_else(TaskCompletionRate > .790, Effective_Overall_TopboxMean*0.9*100,
if_else(TaskCompletionRate > .650, Effective_Overall_TopboxMean*0.8*100,
if_else(TaskCompletionRate > .500, Effective_Overall_TopboxMean*0.6*100,
if_else(TaskCompletionRate > .250, Effective_Overall_TopboxMean*0.4*100,
Effective_Overall_TopboxMean*0.2*100))))))
#result
UXQ_Boxes2 %>% select(last_col())
Adding missing grouping variables: `segment`
# A tibble: 2 x 2
# Groups: segment [2]
segment UWeightedEffective
<chr> <dbl>
1 Developers 349.
2 lowcode 180我注意到在代码的最后乘以6,我把它移到了代码的开头。我还折叠了最后一个条件UXQ_Boxes$TaskCompletionRate < .260),因为前一个条件是UXQ_Boxes$TaskCompletionRate > .250),所以它没有任何意义。我将UXQ_Boxes$Effective_Overall_TopboxMean)*.2)*100放到了else中
https://stackoverflow.com/questions/68931183
复制相似问题