As mentioned in the comments, merging your two data frames together will be helpful here.
This is one approach using the tidyverse
packages.
First, extract the friend's name from title
using an appropriate regex pattern.
Then, merge with left_join
the two data frames, and for each id
and title
, check to see if the name
is included in friend_name
(from the friends
data frame).
library(tidyverse)
likes %>%
mutate(name = sub(".*likes (.+?)('s)? post.", "\\1", title)) %>%
left_join(friends) %>%
group_by(id, title, name) %>%
summarise(likefriendpost = +any(name %in% friend_name))
Edit: Here is a data.table
version.
First, initialize likefriendpost
to zero in likes
table, and extract friend_name
in a similar fashion. Then, use setkey
for sorting and preparing to join on both id
and friend_name
. Finally, change likefriendpost
to 1 where joined on id
and friend_name
.
library(data.table)
setDT(likes)
setDT(friends)
likes[, `:=`(likefriendpost = 0, friend_name = sub(".*likes (.+?)('s)? post.", "\\1", title))]
setkey(likes, id, friend_name)
setkey(friends, id, friend_name)
likes[friends, on = .(id, friend_name), likefriendpost := 1]
Output
id title name likefriendpost
<dbl> <chr> <chr> <int>
1 1 1 likes A's post. A 1
2 1 1 likes his own post. his own 0
3 2 2 likes G's post. G 0
4 3 3 likes B's post. B 0
5 3 3 likes F's post. F 1
Data
likes <- structure(list(id = c(1, 1, 2, 3, 3), title = c("1 likes A's post.",
"1 likes his own post.", "2 likes G's post.", "3 likes F's post.",
"3 likes B's post.")), class = "data.frame", row.names = c(NA,
-5L))
friends <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L), friend_name = c("A",
"B", "C", "B", "E", "H", "A", "F")), class = "data.frame", row.names = c(NA,
-8L))