R — Logical grep on multiple variables within data frame

。_饼干妹妹 提交于 2019-12-12 02:49:07

问题


I am interested in performing a string search using logical grep (grepl) in R, with multiple string patterns, and would like to apply this function to several variables (columns) in my data frame. I believe that one of the apply functions is going to be well-suited to this task, but I am not entirely sure how to get it to work correctly. Please find an example (toy) included below:

v.grepl <- Vectorize(grepl)
pattern <- "^330|^334|^335|^343|^359|^740|^741|^742"
data <- structure(list(recnum = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                   pr1_3 = c("334", "550", "600", "812", "748", "968", "123", "456", "789", "821"),
                   pr2_3 = c("350", "222", "367", "", "", "", "", "", "", ""),
                   pr3_3 = c("857", "", "", "", "", "", "", "", "", ""), 
                   pr4_3 = c("359", "740", "336", "400", "", "", "", "", "", ""),
                   pr5_3 = c("800", "", "", "", "", "", "", "", "", "")),
              .Names = c("recnum", "pr1_3", "pr2_3", "pr3_3", "pr4_3", "pr5_3"),
              row.names = c(1L, 2L, 3L,4L, 5L, 6L, 7L, 8L, 9L, 10L),
              class = "data.frame")

data$check <- apply(data, 2, v.grepl(pattern, data[c('pr1_3', 'pr2_3', 'pr3_3', 'pr4_3', 'pr5_3')]))

The last line of code throws the following error:

Error in match.fun(FUN) : 
'v.grepl(pattern, data[c("pr1_3", "pr2_3", "pr3_3", "pr4_3", "pr5_3")])' is not a function, character or symbol

Does anyone have any ideas for how to fix this code so that it adds a new variable within the data dataframe (called check) that flags whether each row has a pr1_3 through pr5_3 that matches the strings included in pattern?

Thanks!

来源:https://stackoverflow.com/questions/35370739/r-logical-grep-on-multiple-variables-within-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!