I have the following list, and I want to create a data.frame that holds every possible "path", when the numeric value of the arrays is> 0.
This is the list:
> ABBCCD2
$A1
$A1$B1
D1 D2
C1 0.233 0.078
C2 0.039 0.039
$A1$B2
D1 D2
C1 0.083 0.028
C2 0.056 0.056
$A1$B3
D1 D2
C1 0.083 0.028
C2 0.056 0.056
$A2
$A2$B1
D1 D2
C1 0.100 0.033
C2 0.017 0.017
$A2$B2
D1 D2
C1 0 0
C2 0 0
$A2$B3
D1 D2
C1 0 0
C2 0 0
And this is the result I want:
> res
FUN INTC INTB INME prob
1 A1 B1 C1 D1 0.233
2 A1 B1 C1 D2 0.078
3 A1 B1 C2 D1 0.039
4 A1 B1 C2 D2 0.039
5 A1 B2 C1 D1 0.083
6 A1 B2 C1 D2 0.028
7 A1 B2 C2 D1 0.056
8 A1 B2 C2 D2 0.056
9 A1 B3 C1 D1 0.083
10 A1 B3 C1 D2 0.028
11 A1 B3 C2 D1 0.056
12 A1 B3 C2 D2 0.056
13 A2 B1 C1 D1 0.100
14 A2 B1 C1 D2 0.033
15 A2 B1 C2 D1 0.017
16 A2 B1 C2 D2 0.017
I have solved it with for loops, but it is not efficient, since the real problem that I have there are 15 million possible paths, and it can take several days to solve it. This is the code I have made:
m <- 0
# creamos dataframe vacio
res <- data.frame(FUN=character(),INTC=character(),INTB=character(),INME=character(),prob=numeric())
for(i in 1:length(ABBCCD2)) { # A
for (j in 1:length(ABBCCD2[[1]])) { # B
for(k in 1:nrow(ABBCCD2[[1]][[1]])) { # C
for(f in 1:ncol(ABBCCD2[[1]][[1]])) { # D
# solo guardamos las prob > 0
if(ABBCCD2[[i]][[j]][k,f] > 0) {
# contador de caminos con probabilidad no-cero
m <- m + 1
# creamos la fila del data frame correspondiente y vamos rellenando
res[m,] <- data.frame(FUN=names(ABBCCD2[i]), INTC=names(ABBCCD2[[i]][j]), INTB=rownames(ABBCCD2[[i]][[j]])[k],
INME = colnames(ABBCCD2[[i]][[j]])[f] , prob = ABBCCD2[[i]][[j]][k,f] )
}else{
}
}
}
}
}
Any ideas to solve it more efficiently?
Thank you all