I created a function clique_function that selects variables from two data frames object_list and clique_list based on conditions and returns a new data frame from that clique_object.
Input:
clique_list is a list of cliques (groups of three nodes in a network) where each column represents a clique with three nodes composing it.
clique_list <- structure(c("ND1", "IS1", "IS3", "IS1", "IS3", "IS2", "ND2", "ND1", "IS1"), .Dim = c(3L, 3L)) object_list is a matrix with the nodes as rows and the occurrence of different object types as column.
object_list <- structure(list(CA1 = c(0.159159159159159, 0.222222222222222, 0.25, 0.115384615384615, 0.311111111111111, 0.1285140562249, 0.214132762312634, 0.413461538461538, 0.183333333333333, 0.4, 0.4375, 0.167778836987607, 0.25, 0.5, 0.166666666666667, 0.181818181818182, 0.21580547112462, 0.0792452830188679, 0.424657534246575, 0, 0 ), CA11 = c(0.00600600600600601, 0, 0, 0, 0, 0.00401606425702811, 0.012847965738758, 0, 0.05, 0, 0, 0, 0, 0, 0, 0, 0.00911854103343465, 0.0113207547169811, 0.0410958904109589, 0, 0), CA111 = c(0, 0, 0, 0, 0, 0, 0.00499643112062812, 0, 0.0333333333333333, 0, 0, 0, 0, 0, 0, 0, 0.0060790273556231, 0.00754716981132075, 0.0273972602739726, 0, 0), CA1111 = c(0, 0, 0, 0, 0, 0, 0.000713775874375446, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CA1113 = c(0, 0, 0, 0, 0, 0, 0.000713775874375446, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CA1115 = c(0, 0, 0, 0, 0, 0, 0.000713775874375446, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CA1116 = c(0, 0, 0, 0, 0, 0, 0, 0, 0.0166666666666667, 0, 0, 0, 0, 0, 0, 0, 0.00303951367781155, 0.00377358490566038, 0, 0, 0), CA1117 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0136986301369863, 0, 0), CA112 = c(0, 0, 0, 0, 0, 0, 0.00285510349750178, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CA1122 = c(0, 0, 0, 0, 0, 0, 0.00142755174875089, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c("ND5", "ND6", "ND8/ND10", "ND3", "ND7", "ND2", "ND1", "ND4", "KB3/KB4/KB5", "KB1", "KB2", "IS1", "KB9", "KB7/KB8", "KB6", "IS4", "IS3", "IS2", "KB12/KB14", "KB13", "IS5")) The function clique_function is supposed to loop around object_list and select the variable type (column) such as CA1 of the three nodes from clique_list.
clique_function <- function(clique_list, type, object_list) { for (i in 1:ncol(clique_list)) { clique_object <- subset(object_list, row.names(object_list) %in% clique_list[, i], colnames(object_list) == type) } return(clique_object) } Expected output clique_object is a subset of object_list showing the occurrence of a chosen object type type for all the cliques in clique_list.
For exemple:
clique_object <- structure(list(cliques = c("ND1", "IS1", "IS3", "ND2", "IS1", "IS3", "ND1", "IS4", "IS3", "ND1", "IS1", "IS3", "ND1", "IS1", "IS8", "ND3", "IS1", "IS3", "ND1", "IS1", "IS3"), CA1 = c(0.0007137759, 0.0047664442, 0.009118541, 0.0007137759, 0.0047664442, 0.009118541, 0.0007137759, 0.0047664442, 0.009118541, 0.0007137759, 0.0047664442, 0.009118541, 0.0007137759, 0.0047664442, 0.009118541, 0.0007137759, 0.0047664442, 0.009118541, 0.0007137759, 0.0047664442, 0.009118541 )), class = "data.frame", row.names = c(NA, -21L)) The function works properly if instead of return(clique_object) I put print(clique_object). In the first case I get the full list from the function looping around the data frames. But with return(clique_object) I only get the result for the first clique in clique_list.
I would like the function to output the full result as a data frame.
Thank you.