I have been trying to write a function or use the apply family to select the rows in a data frame that contain the words I'm looking for and mark them like a tag. A row can have several tags. Can someone please help me, I have been stuck for a while.
If my question is unclear or if there is an answer somewhere else please guide me in the right direction. Much appreciated!
require(stringr)
require(dplyr)
df <- data.frame(sentences, rnorm(length(sentences)))
old = df %>% filter(str_detect(sentences, 'old')) %>% mutate(w = factor("old"))
new = df %>% filter(str_detect(sentences, 'new')) %>% mutate(w = factor("new"))
boy = df %>% filter(str_detect(sentences, 'boy')) %>% mutate(w = factor("boy"))
girl = df %>% filter(str_detect(sentences, 'girl')) %>% mutate(w = factor("girl"))
tags <- bind_rows(old, new, boy, girl)
So i want to choose a finite number of words for example:
tags <- c('bananas', 'apples', oranges)
And I want the result to be a data.frame with new columns for every word I have chosen. If the row contains one of the words I have chosen, the column for that words should be TRUE och marked somehow. Something like
Sentences bananas apples oranges
sentence1 TRUE
sentence2 TRUE
sentence3 TRUE
sentence4 TRUE
sentence5 TRUE TRUE
or
Sentences tag1 tag2
sentence1 bananas
sentence2 apples
sentence3 bananas
sentence4 oranges
entences5 apples oranges
Or something like that. Please let me know if I can explain more clearly.