Not clear what you trying to achieve but this should get you started...
output <-
data.frame(
Word1="budget",
Word2="laptop",
Word3=sapply(1:nrow(df),function(i){
x <- as.character(df[i,1:3])
x[ !x %in% c("budget","laptop")]}),
Total.Impressions=df$Total.Impressions
)
EDIT: Possibly there is a prettier way of doing this, but this should work:
output <-
data.frame(
Word1="budget",
Word2="laptop",
Word3=sapply(1:nrow(df),function(i){
x <- as.character(df[i,1:3])
res <- x[ !x %in% c("budget","laptop")]
#check if result is not empty
if(length(res)==0){
res <- aggregate(x,list(x),length)
res[ res$x==2, 1]}
else res
}),
Total.Impressions=df$Total.Impressions
)