Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I automate the code to run for each row entry in the dataset in R?

Let’s say I have this data frame of several random sentences

Sentences<-c("John is playing a video game at the moment","Tom will cook a delicious meal later",
     "Kyle is with his friends watching the game",
     "Diana is hosting her birthday party tomorrow night"
     )
df<-data.frame(a)

keywords<-c("game","is","will","meal","birthday","party")

And I have a vector of key words. I need to create a new column in the data frame with only keywords mentioned in the sentence appearing.

na.omit(str_match(df[n,],keywords))

I have constructed this line of code which returns keywords that were used in those sentences (n stands for row number). How do I automate this code to be applied for each row?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We could use str_extract_all from stringr package for this:

library(dplyr)
library(stringr)
df %>% 
  mutate(new_col = str_extract_all(Sentences, paste(keywords, collapse = "|")))
                                           Sentences             new_col
1         John is playing a video game at the moment            is, game
2               Tom will cook a delicious meal later          will, meal
3         Kyle is with his friends watching the game        is, is, game
4 Diana is hosting her birthday party tomorrow night is, birthday, party
      
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading