Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- imsbasics::clc()
- apply_rules <- function(data, rules) {
- for (i in 1:length(rules)) {
- rules_now <- unlist(strsplit(rules[i], ", "))
- for (j in 1:length(rules_now)) {
- data[i] <- apply_rule(data[i], rules_now[j])
- }
- }
- return(data)
- }
- apply_rule <- function(data, rule) {
- rule <- get(rule)
- data <- rule(data)
- return(data)
- }
- rule1 <- function(data) {
- data * 2
- }
- rule2 <- function(data) {
- data + 1
- }
- rule3 <- function(data) {
- data ^ 3
- }
- # Interaktive Tests, einzelne Rules, gepiped.
- library(magrittr)
- data <- c(1,2,3)
- apply_rule(data, "rule1")
- apply_rule(data, "rule1") %>% apply_rule("rule2")
- apply_rule(data, "rule2") %>% apply_rule("rule1")
- apply_rule(data, "rule3")
- apply_rule(data, "rule3") %>% apply_rule("rule2") %>% apply_rule("rule1")
- # Hinterlegung der Rules für jedes Element, automatisches dispatching mit apply_rules()
- df <- data.frame(
- data <- c(1,2,3),
- rules <- c("rule1", "rule1, rule2, rule3", "rule3, rule2"),
- stringsAsFactors = FALSE
- )
- apply_rules(df$data, df$rules)
- # [1] 2 125 28
Add Comment
Please, Sign In to add comment