Filter a column which contains several keywords

前端 未结 5 1277
Happy的楠姐
Happy的楠姐 2020-12-12 01:53

I am trying to filter a column which contains several keywords (in this example dog and cat) but I am having problems as only the first element is being used.



        
5条回答
  •  旧时难觅i
    2020-12-12 02:21

    Try this:

    dfilter <- df1[sapply(filter1, function(x) grep(x,df1$type)),]
    

    It's complaining because your filter is a vector and grep wants a string.

    Edit:

    From this answer:

    dfilter <- df1[df1$type %in% grep(paste(filter1, collapse="|"), df1$type, value=TRUE), ]
    

提交回复
热议问题