在data.frame x中有两列名为"preferred_cat“和"var_nm”。我希望创建一个名为"preferred_tag“的第三列,其值为1或0。
1-如果preferred_cat(用于ex )是var_nm的子集(用于jdsajqq)或
0-如果preferred_cat(用于ex )不是var_nm的子集( ex )
x <- x %>% mutate(preferred_tag=ifelse(grepl(preferred_cat,var_nm,fixed=TRUE),1,0))然而,我收到了这样的警告:
Warning message:
In grepl(preferred_cat, var_nm, fixed = TRUE) :
argument 'pattern' has length > 1 and only the first element will be used这一警告意味着什么,我如何避免它?
发布于 2018-04-09 00:27:38
grepl不会使用字符串的向量。您可以使用来自map2的purrr
首先创建一个新函数,如果有子字符串,则返回1,否则为0。
new_func <- function(x,y){
if(grepl(x,y,fixed=TRUE)){
check <- 1
} else{
check <- 0
}
check
}现在,将此函数map2到每对字符串:
library(purrr)
x <- x %>%
mutate(preferred_tag=map2(referred_cat, var_nm, new_func))https://stackoverflow.com/questions/49724035
复制相似问题