将列名传递给自定义函数

时间:2016-08-17 18:45:21

标签: r

我正在尝试制作一个自定义函数来根据格式,数字集和问题的问题来纠正一些答案。为此,我必须从三个不同的列中识别这三个变量,然后检查对应行的答案。

考虑下一个数据框作为我正在使用的数据框的示例。

df = data.frame(ID = 1:10,
            Formato_SCREE1 = c("FN", "FN", "FN", "PR", "PR", "FN", "PR", "PR", "FN", "PR"),
            Problema_SCREE1 = c("UNIDAD", "DECIMA", "UNIDAD", "UNIDAD", "DECIMA", "DECIMA", "UNIDAD", "DECIMA", "DECIMA", "UNIDAD"),
            Set_SCREE1 = c("SET4", "SET1", "SET4", "SET3", "SET3", "SET4", "SET3", "SET2", "SET1", "SET2"),
            Resp_SCREE1 = c(0.7777778,  0.5000000,  0.7777778,  0.7142857,  2.5000000,  0.7777778, 0.7142857, 0.2857143, 0.3333333, 110.1111111))

ID Formato_SCREE1 Problema_SCREE1 Set_SCREE1 Resp_SCREE1
1   1             FN          UNIDAD       SET4   0.7777778
2   2             FN          DECIMA       SET1   0.5000000
3   3             FN          UNIDAD       SET4   0.7777778
4   4             PR          UNIDAD       SET3   0.7142857
5   5             PR          DECIMA       SET3   2.5000000
6   6             FN          DECIMA       SET4   0.7777778
7   7             PR          UNIDAD       SET3   0.7142857
8   8             PR          DECIMA       SET2   0.2857143
9   9             FN          DECIMA       SET1   0.3333333
10 10             PR          UNIDAD       SET2 110.1111111

我的第一个纠正答案的解决方案如下:

temp1 = df %>% 
  filter(Set_SCREE1 == "SET1") %>% 
  mutate(Error_SCREE1 = ifelse( Formato_SCREE1 == "PR" & Problema_SCREE1 == "DECIMA", Correct_answer_PR_DECIMA_SET1 - Resp_SCREE1,
                                ifelse( Formato_SCREE1 == "PR" & Problema_SCREE1 == "UNIDAD", Correct_answer_PR_UNIDAD_SET1 - Resp_SCREE1,
                                        ifelse(Formato_SCREE1 == "FN" & Problema_SCREE1 == "DECIMA", Correct_answer_FN_DECIMA_SET1 - Resp_SCREE1,
                                               ifelse(Formato_SCREE1 == "FN" & Problema_SCREE1 == "UNIDAD", Correct_answer_FN_UNIDAD_SET1 - Resp_SCREE1, 0))))) %>% 
  select(ID, Error_SCREE1)

正确答案:

Correct_answer_PR_DECIMA_SET1 = 1
Correct_answer_PR_UNIDAD_SET1 = 2
Correct_answer_FN_DECIMA_SET1 = 3
Correct_answer_FN_UNIDAD_SET1 = 4

这很好但我必须重复四次(对于每个Set_SCREE1),然后再重复四次(对于...... SCREE2,...... SCREE3和...... SCREE4)。这导致了16块代码。太多的线条和难以阅读的东西。

然后我尝试创建一个可以在更少的行中执行相同操作的函数,并最终得到:

error_calculator = function(data, set, formato, problema, correcta) {

temp = data %>% 
filter(Set_SCREE1 == set) %>% 
mutate(Error_SCREE1 = ifelse( formato == "PR" & problema == "DECIMA", correcta - Resp_SCREE1, 
                          ifelse( formato == "PR" & problema == "UNIDAD", correcta - Resp_SCREE1, 
                                      ifelse( formato == "FN" & problema == "DECIMA", correcta - Resp_SCREE1, 
                                              ifelse( formato == "FN" & problema == "UNIDAD", correcta - Resp_SCREE1, 0))))) %>%

select(ID, Error_SCREE1)

return(temp)
}

temp1 = error_calculator(df, "SET1", "Formato_SCREE1", "Problema_SCREE1", "Correct_answer_PR_DECIMA_SET1")  

此函数的问题是只能在filter()行之前工作,然后才能完成以下ifelses的条件,因此Error_SCREE1列填充为“0”。 我的第一个想法是你不能以我正在做的方式将列名传递给函数。但根据this,你实际上可以。所以我无法确定为什么列名不被识别。

0 个答案:

没有答案