I have a question that is somewhat similar to this, but this solution did not work for me: MATCH function in r
I have two data frames that look like this:
df1
query page
apple site.com
apple site.com/apples
banana site.com/bananas
bad apples site.com/apples'
df2
page sessions
site.com 20
site.com/apples 30
site.com/bananas 50'
I need a new column in df1 that shows sessions. When I tried using match() per the discussion above, the entire column returned as "NA." This was my code:
df1$sessions <- df2$sessions[match(df1$page, df2$page)]
I also tried merge:
df_merged = merge(df1, df2, by="page", all.x = TRUE)
It seems like a left join makes sense here, perhaps using sqldf + function(), but I can't seem to formulate this correctly and I'm not entirely sure if function is even needed or if sqldf can do this on its own. It would be fine to show "NA" in df1 wherever there is not a match but I'm getting "NA" even when there should be a match. I tried this, but to no avail:
df_merged <- left_join(df1, df2 by='page')