Grepl through multiple specified columns

I want to create a new column in my data frame that is either TRUE or FALSE depending on whether the term has two specified columns. Here are some sample data:

AB <- c('CHINAS PARTY CONGRESS','JAPAN-US RELATIONS','JAPAN TRIES TO')
TI <- c('AMERICAN FOREIGN POLICY', 'CHINESE ATTEMPTS TO', 'BRITAIN HAS TEA')
AU <- c('AUTHOR 1', 'AUTHOR 2','AUTHOR 3')
M  <- data.frame(AB,TI,AU)

I can do this for one column or the other, but I cannot figure out how to do this for both. In other words, I do not know how to combine these two lines, which will not mutually correspond with each other.

M$China <- mapply(grepl, "CHINA|CHINESE|SINO", x=M$AB)
M$China <- mapply(grepl, "CHINA|CHINESE|SINO", x=M$TI)

It is important that I specify the columns, I can not select the entire data.frame file. I searched for other similar questions, but none of them apply to my case, and I could not adapt existing examples. Here's what would be helpful to me:

M$China <- mapply(grepl, "CHINA|CHINESE|SINO", x=(M$AB|M$TI)
+4
source share
2 answers

Using:

M$China <- !!rowSums(sapply(M[1:2], grepl, pattern = "CHINA|CHINESE|SINO"))

gives:

> M
                     AB                      TI       AU China
1 CHINAS PARTY CONGRESS AMERICAN FOREIGN POLICY AUTHOR 1  TRUE
2    JAPAN-US RELATIONS     CHINESE ATTEMPTS TO AUTHOR 2  TRUE
3        JAPAN TRIES TO         BRITAIN HAS TEA AUTHOR 3 FALSE

What does it do:

  • sapply(M[1:2], grepl, pattern = "CHINA|CHINESE|SINO") AB TI , ("CHINA|CHINESE|SINO").
  • sapply -call TRUE/FALSE:

            AB    TI
    [1,]  TRUE FALSE
    [2,] FALSE  TRUE
    [3,] FALSE FALSE
    
  • rowSums , TRUE - .

  • !! rowSums, rowSums -call TRUE eros FALSE.
+3

, Map , pattern, a list logical, Reduce a logical |

M$China <- Reduce(`|`, Map(grepl, "CHINA|CHINESE|SINO", M))
M
#                     AB                      TI       AU China
#1 CHINAS PARTY CONGRESS AMERICAN FOREIGN POLICY AUTHOR 1  TRUE
#2    JAPAN-US RELATIONS     CHINESE ATTEMPTS TO AUTHOR 2  TRUE
#3        JAPAN TRIES TO         BRITAIN HAS TEA AUTHOR 3 FALSE

tidyverse

library(tidyverse)
M %>%
   mutate_all(funs(str_detect(., "CHINA|CHINESE|SINO")))  %>% 
   reduce(`|`) %>%
   mutate(M, China = .)
+1

Source: https://habr.com/ru/post/1691233/


All Articles