I have strings a
and b
that compose my data
. My purpose is to obtain a new variable that contains repeated words.
a = c("the red house av", "the blue sky", "the green grass")
b = c("the house built", " the sky of the city", "the grass in the garden")
data = data.frame(a, b)
Based on this answer I can get the logical of those that are repeated with duplicated()
data = data%>% mutate(c = paste(a,b, sep = " "),
d = vapply(lapply(strsplit(c, " "), duplicated), paste, character(1L), collapse = " "))
Yet I am not able to obtain the words. My desired data should be something like this
> data.1
a b d
1 the red house av the house built the house
2 the blue sky the sky of the city the sky
3 the green grass the grass in the garden the grass
Any help on the function above would be highly appreciated.