How to compare execution time for mutate case_when-function and if-else-function?

时间:2018-02-21 11:49:53

标签: r if-statement execution-time mutate

for my seminar I have to find a way of showing how "efficient" or "smart" programming looks like. For example, I know that apply is more efficient than a for loop in terms of the time needed to execute 100 000 or more iterations. For the next session I would like to show that mutate and case_when are more efficient than if-else for conditional programming. I have this dataset

  var1 var2 var3
1 yes  no
2 no   no
3 yes  yes
4 no   yes
5 yes  yes

and used this mutate function to fill in var3:

dat1 <- dat2 %>%
         mutate(var3 = case_when(
         var1 %in% "no"  & var2 %in% "no"   ~ "product1",
         var1 %in% "yes" & var2 %in% "no"   ~ "product2",
         var1 %in% "no"  & var2 %in% "yes"  ~ "product3",
         var1 %in% "yes" & var2 %in% "yes"  ~ "product4"))                             

My question is, what would be the way to get the system times for executing the above function for 100 000 cases and what would be the corresponding if-else statement? Any ideas?

0 个答案:

没有答案