Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

Rule tables to avoiding nested ifelse statements

$
0
0

Idea is to have manageable approach to define rules from some table:

library(data.table)

a <- data.table(rule = c("rule1", "rule2", "rule3"),
                bool = c(T,T,F))

a
#     rule  bool
# 1: rule1  TRUE
# 2: rule2  TRUE
# 3: rule3 FALSE

ifelse(a[rule == "rule1", bool] & a[rule == "rule2", bool] & a[rule == "rule3", bool], 1,
       ifelse(a[rule == "rule1", bool] & a[rule == "rule2", bool], 2,
              ifelse(a[rule == "rule2", bool] & a[rule == "rule3", bool], 3, 4)))
# [1] 2

Obviously this is not very sustainable or readable as I keep adding rules. What would an alternative to ifelse here?


Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>