I have this list
mylist = list(structure(c(1L, 2L, 4L, 6L, 7L, 8L, 11L, 13L, 14L, 16L), env = <weak reference>, graph = "8e4abfef-5278-11e8-bbcf-c5dd5cc2b8d4", class = "igraph.vs"),
structure(c(9L, 17L), env = <weak reference>, graph = "8e4abfef-5278-11e8-bbcf-c5dd5cc2b8d4", class = "igraph.vs"),
structure(c(12L, 15L, 18L, 20L), env = <weak reference>, graph = "8e4abfef-5278-11e8-bbcf-c5dd5cc2b8d4", class = "igraph.vs"))
and a dataframe
mydf = structure(list(session_id = c(861L, 861L, 862L, 862L, 868L, 868L,
871L, 871L, 874L, 874L, 875L, 875L, 877L, 877L, 879L, 879L, 879L,
880L, 880L, 883L, 883L, 884L, 884L, 886L, 886L, 892L, 892L, 894L,
894L, 895L, 895L, 897L, 897L, 898L, 898L, 899L, 899L, 900L, 900L,
900L, 900L, 901L, 901L, 902L, 902L, 904L, 904L, 907L, 907L, 908L
), requestId = c(11, 11, 3, 1, 14, 17, 6, 11, 16, 20, 12, 16,
11, 8, 6, 1, 15, 4, 4, 10, 1, 17, 6, 5, 1, 5, 3, 6, 6, 13, 6,
3, 5, 18, 1, 6, 17, 11, 21, 3, 16, 14, 12, 4, 1, 10, 17, 20,
21, 10)), .Names = c("session_id", "requestId"), row.names = c(NA,
-50L), class = c("tbl_df", "tbl", "data.frame"))
Now I want to write a function recommenSet which will take mylist and pages as its arguments and apply LCS algo from library qualv on list items and pages grouped by sessions ids and where it get max LLCS (i.e. its length) for each corresponding session_id
.
pages = mydf%>% group_by(session_id) %>% do(predicSet = recommenSet(mylist,as.vector(.$requestId)))
recommenSet = function(mylist,pages){
recSet = lappy(mylist,function(x){
a = x[order(x)]
a = as.character(a)
b = pages[order(pages)]
b = as.character(b)
LCS(a,b)
#give me setdiff of a and b where LLCS is max among mylist items
setdiff(a,b)
})
#recSet = unlist(recSet);
#recSet = unique(recSet);
}
return(recSet)
}
e.g. for session_id = 877 corresponding pages are c(11,8) so we arrange them in asc order c(8,11)
Now we apply LCS between each list of mylist(3 lists) and sessions pages(grouped by session_ids) and look for which list it gives maximum LLCS for corresponding session_ids and then return setdiff() b/w the sets which has max LLCS.