Getting specific operations in R for loop based on list values size -
i have following code:
t1 <- list(t1, id_list=list(), values=c()) t2 <- list(t2, id_list=list(), values=c()) values1 = as.numeric(as.vector(values1)) t1$values = values1 n.id.list1 = length(sort(unique(values1))) order.list1 = sort(unique(values1)) ( in 1:n.id.list1) { id1 = order.list1[i] t1$id_list[[i]] <- which(values1==id1) }
same t2:
values2 = as.numeric(as.vector(values2)) t2$values = values2 n.id.list2 = length(sort(unique(values2))) order.list2 = sort(unique(values2)) ( in 1:n.id.list2) { id2 = order.list2[i] t2$id_list[[i]] <- which(values2==id2) } <- length(t1$id_list) b <- length(t2$id_list) size_lists<-matrix(0,2,max(a,b))
lists size:
for (i in 1:a){ size_lists[1,i] <- length(t1$id_list[[i]]) } (j in 1:b){ size_lists[2,j] <- length(t2$id_list[[j]]) }
number of matching between lists:
for (i in 1:a) { (j in 1:b){ size_matching[i,j] <- length(intersect(t1$id_list[[i]], t2$id_list[[j]]) } }
and calculate conditional probabilities between lists:
for (i in 1:a) { (j in 1:b) { if(size_lists[1,i] > 0 & (size_lists[2,j]/size_lists[1,i] > 0.5)) { match[i,j] <- round((size_matching[i,j]/size_lists[1,i]),digits=2) } else { match[i,j] <- 0 } } }
i want calculate these match[i,j] specific list sizes, defined above, simplify process. idea order lists decreasing size , calculate match
top k lists size, can´t manage it.
i tried ordering values1
, values2
using table
, doesn´t worked had in mind. tried include ratio between list sizes (the & (size_lists[2,j]/size_lists[1,i] > 0.5)
above), threshold value depends on lists.
edit:
concerning comment of mrflick:
i have 2 lists, l1 , l2, @ 2 different timepoints, t1 , t2. @ each timepoint, input gives membership of lists('values' lists), ie, class, gives belonging group in.
e.g.:
color values1(t1) values2(t2) white 1 2 blue 2 2 red 1 1 orange 2 na violet 2 4 brown na 2 black 1 3 purple 3 3 green 3 2 grey 2 2 yellow 1 na gold na 1 pink 1 1 silver 3 3 darkred 1 3
the desired output list describing transitions between t1 , t2. problem want limit calculations on conditional probabilities (it calculates them every pair of class) based on lists size. instance, class higher membership in t1 runs match[i,j] top5 highest classes in size in t2. said, tried using ratio between list sizes, doesn´t work.
Comments
Post a Comment