ifelse语句超出限制

时间:2017-07-25 22:14:49

标签: r regex if-statement

问题:我编写了一个包含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

2 个答案:

答案 0 :(得分:3)

你可以将你的模式放在一个命名向量中(注意Other = "",当你的模式都没有与字符串匹配时,这是一个后退):

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

然后遍历模式并检查字符串是否包含pattern:

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

最后建立新列购买检查匹配模式的名称,如果没有匹配,则返回其他:

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

答案 1 :(得分:0)

您可以按顺序执行它们,而不是嵌套条件。使用for循环:

# Store the find-replace pairs in a data frame

word_map <- data.frame(pattern = c("home", "school", "work"),
                       replacement = c("At Home", "At School", "At Work"), 
                       stringsAsFactors = FALSE)

word_map
pattern replacement
1    home     At Home
2  school   At School
3    work     At Work

# Iterate through the pairs

for ( i in 1:nrow(word_map) ) {

    pattern     <- word_map$pattern[i]
    replacement <- word_map$replacement[i]

    mydf$location <- ifelse(grepl(pattern, mydf$location, ignore.case = TRUE), replacement, mydf$location)
}

mydf
   answer  location
1       4   At Home
2       4   At Home
3       1   At Home
4       5 At School
5       1 At School
6       2 At School
7       5   At Work
8       2   At Work
9       1   At Work
10      3   At Work