问题:我编写了一段巨大的代码,其中包含超过100个ifelse语句,只是为了了解ifelse语句的数量受到限制:超过50个会引发错误。无论如何,我知道有一种更有效的方式来做我想做的事情。

目标:尝试编写一个函数来将字符串的许多变体(请参见下面的示例)重新编码为清晰的类别(例如以下)。我使用str_detect给出T / F,然后根据响应更改为正确的类别。如何在没有超过100个ifelse语句的情况下执行此操作(我的类别更多)。

例:

mydf <- data_frame(answer = sample(1:5, 10, replace = T),
                location = c("at home", "home", "in a home",
"school", "my school", "School", "Work", "work",
                         "working", "work usually"))

loc_function <- function(x) {
  home <- "home"
  school <- "school"
  work <- "work"
  ifelse(str_detect(x, regex(home, ignore_case = T)), "At Home",
     ifelse(str_detect(x, regex(school, ignore_case = T)), "At
School",
            ifelse(str_detect(x, regex(work, ignore_case = T)), "At
Work", x)))
}

### Using function to clean up messy strings (and recode first column too) into clean categories
mycleandf <- mydf %>%
  as_data_frame() %>%
  mutate(answer = ifelse(answer >= 2, 1, 0)) %>%
  mutate(location = loc_function(location)) %>%
  select(answer, location)

mycleandf

# A tibble: 10 x 2
   answer  location
    <dbl>     <chr>
 1      1   At Home
 2      1   At Home
 3      1   At Home
 4      1 At School
 5      1 At School
 6      1 At School
 7      1   At Work
 8      0   At Work
 9      1   At Work
10      0   At Work

最佳答案

您可以将模式放入命名的向量中(注意Other = "",这是当所有模式都不与字符串匹配时的回退):

patterns <- c("At Home" = "home", "At School" = "school", "At Work" = "work", "Other" = "")


然后循环遍历该模式,并检查字符串是否包含模式:

match <- sapply(patterns, grepl, mydf$location, ignore.case = T)


最后,建立新的一列buy,检查要替换的匹配模式的名称,如果没有匹配,请回到Other:

mydf$clean_loc <- colnames(match)[max.col(match, ties.method = "first")]
mydf

# A tibble: 10 x 3
#   answer     location clean_loc
#    <int>        <chr>     <chr>
# 1      3      at home   At Home
# 2      3         home   At Home
# 3      3    in a home   At Home
# 4      3       school At School
# 5      2    my school At School
# 6      4       School At School
# 7      5         Work   At Work
# 8      1         work   At Work
# 9      2      working   At Work
#10      1 work usually   At Work

关于r - ifelse语句超出限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45314395/

10-08 21:10