如果值匹配向量

时间:2018-04-26 02:25:59

标签: r for-loop

我正在尝试以这种形式创建数据集:

enter image description here

我无法弄清楚for循环有什么问题。

还有其他更简单的解决方案来解决我在R中所做的事情吗?

equality_vector <- c("CEO", "COO","CTO","CDO","CFO","CTO","CIO")

Position <- c("Retired CEO", "COO/Board member", "Operator")
Role <- c(NA, NA, NA)
position1<- cbind.data.frame(Position, Role)

for(pos in position1$Position)
{
  a <- NA
  for (var in equality_vector) {
  if (grepl(var,pos)) {
  a <- var
}
break
}
position1$Role[position1$Position == pos] <- a
}

2 个答案:

答案 0 :(得分:0)

这将完成工作。它避免了一个循环(另一个循环隐藏在map_int语句中.R中的大多数程序员都会避免像瘟疫这样的循环。它们在R中往往很慢。你可能想要阅读其中的一些功能并逐步完成每个人的工作。

library(tidyverse)
match_positions <- function(position) {
  outer(position, equality_vector,  str_detect) %>% 
    t() %>% 
    as_data_frame() %>% 
    map_int(.f = function(x) {
      tmp <- which(x)
      if(length(tmp) == 0) tmp <- NA_integer_
      tmp
    })
}

df <- data.frame(Position)
df %>% 
  mutate(
    Role = equality_vector[match_positions(Position)]
  )

答案 1 :(得分:0)

另一种方法可能是

library(dplyr)

data.frame(Position) %>%
  rowwise() %>%
  mutate(Role = ifelse(any(unlist(lapply(equality_vector, function(x) grepl(x, Position)))),
                       equality_vector[unlist(lapply(equality_vector, function(x) grepl(x, Position)))],
                       NA)) %>%
  data.frame() -> df
df

输出为:

          Position Role
1      Retired CEO  CEO
2 COO/Board member  COO
3         Operator <NA>

示例数据:

equality_vector <- c("CEO", "COO", "CTO", "CDO", "CFO", "CTO", "CIO")
Position <- c("Retired CEO", "COO/Board member", "Operator")