R 使用ifelse语句超出限制
问题:我写了一段包含100多条R 使用ifelse语句超出限制,r,regex,if-statement,R,Regex,If Statement,问题:我写了一段包含100多条ifelse语句的巨大代码,但却发现ifelse语句的数量有限:超过50条会抛出错误。无论如何,我知道有一种更有效的方法来做我想做的事情 目标:尝试编写一个函数,将字符串的许多变体(参见下面的示例)重新编码为清晰的类别(例如下面的)。我使用str\u detect给出T/F,然后根据响应转换为正确的类别。如果没有超过100条ifelse语句(我有更多的类别),我怎么能做到这一点呢 例如: mydf <- data_frame(answer = sample(1
ifelse
语句的巨大代码,但却发现ifelse
语句的数量有限:超过50条会抛出错误。无论如何,我知道有一种更有效的方法来做我想做的事情
目标:尝试编写一个函数,将字符串的许多变体(参见下面的示例)重新编码为清晰的类别(例如下面的)。我使用str\u 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
mydf您可以将模式放入命名向量中,(注意Other=”“
,当您的模式与字符串不匹配时,这是一种退步):
模式而不是嵌套条件,您可以按顺序执行它们。对
循环使用:
# 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
#将查找-替换对存储在数据帧中
当你发现自己需要更多的东西时,你应该开始考虑寻找更好的方法。当你达到两把时,你应该开始认为你做错了什么。如果你的手推车满了,要知道你完全搞砸了,需要寻求帮助。您已经达到了垃圾车级别。听起来您想使用case\u when语句,或者使用purr:map()将函数映射到所有单词以使其成为标题case?非常有用。如果我有一个匹配这两个的字符串,我怎么能正确排序呢?例如:“在家然后在工作”我想分类为“在工作”。调整模式或匹配的逻辑顺序?你可以调整模式的顺序,把你想优先考虑的模式放在你不想优先考虑的模式之前。因此,如果你把放在工作中
放在家中的之前
,它会给你工作中的
。
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
# 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