我有以下列表,并希望将每个子列表的每个元素添加到第二个列表的每个元素中,并将累积和向前进行。在我每次移动一个列表时,请注意,列表中删除的元素数量增加了一个。计算非常耗时,并想知道是否有可能制定最后的多个函数,以加快该函数的速度。
DF3 <- list( c ( 12 ,35 ,90 ,33 ,51 ) , c ( 44 , 3 ,88 ,35 ,51 ) , c(12 ,16 ,6 ,10 ,3 ,12 ,2 ,6 ,9 ,4 ,4 ,51 ,13 ,22 ,51 ) , c( 44 ,3 ,37 ,51 ,35 ,51 ) , c( 12, 16 , 6 ,10 , 3 ,12 , 2 , 6 , 9 , 4 , 4 ,51 ,13 , 8 , 3 , 5 , 6 ,51 ) , c( 12 ,16, 6, 10, 3, 37, 51, 35, 51 ) , c( 12, 16, 16, 3, 37, 51, 35, 51 ))
DF3
[[1]]
12 35 90 33 51
[[2]]
44 3 88 35 51
[[3]]
12 16 6 10 3 12 2 6 9 4 4 51 13 22 51
[[4]]
44 3 37 51 35 51
[[5]]
12 16 6 10 3 12 2 6 9 4 4 51 13 8 3 5 6 51
[[6]]
12 16 6 10 3 37 51 35 51
[[7]]
12 16 16 3 37 51 35 51
# Obtain the list of elements to add to the prior list of elements sequentially.
fun <- function (x) tail ( DF3[[x]] , length ( DF3[[x]] ))
S <- lapply ( seq ( length ( DF3 ))[ 1 : ( max (length ( DF3 )))] , fun )[-1]
fun <- function (x) tail (S[[x]] , length( S[[x]])-x)
SS <- c( DF3[[1]][[1]], lapply ( seq ( length ( DF3 )-1), fun ))
# Every result element from previous sum will be added to next list of elements rolling one possition forward for each new list than in the prior list.
D1 <- ( SS[[1]] + SS[[2]] )
a <- seq(nrow(expand.grid(SS[[1]]+SS[[2]],SS[[3]])))
fun <- function (x) sum(expand.grid ( SS[[1]] + SS[[2]] , SS[[3]] )[,1][x], expand.grid ( SS[[1]] + SS[[2]] , SS[[3]] )[,2][x] )
D2 <- unlist(lapply (a , fun))
b <- seq(nrow(expand.grid(D2,SS[[4]])))
fun <- function (x) sum(expand.grid (expand.grid(D2,SS[[4]])[,1][x], expand.grid(expand.grid(D2,SS[[4]]))[,2][x] ))
D3 <- unlist(lapply (b , fun))
c <- seq(nrow(expand.grid(D3,SS[[5]])))
fun <- function (x) sum(expand.grid (expand.grid(D3,SS[[5]])[,1][x], expand.grid(expand.grid(D3,SS[[5]]))[,2][x] ))
D4 <- unlist(lapply (c , fun))
d <- seq(nrow(expand.grid(D4,SS[[6]])))
fun <- function (x) sum(expand.grid (expand.grid(D4,SS[[6]])[,1][x], expand.grid(expand.grid(D4,SS[[6]]))[,2][x] ))
D5 <- unlist(lapply (d , fun))
e <- seq(nrow(expand.grid(D5,SS[[7]])))
fun <- function (x) sum(expand.grid (expand.grid(D5,SS[[7]])[,1][x], expand.grid(expand.grid(D5,SS[[7]]))[,2][x] ))
D6 <- unlist(lapply (d , fun))是否有一种方法可以更简洁地写出最后的表达,也有一种方法可以通过另一种方式加速,因为这是非常缓慢的。
编辑
根据最终结果D6 (cs来自下面的答案)。我想找出每个DF3列表中的位置索引,当顺序累加到221时。
# Taking the answer for the sake of time
f <- function(x, y) rep(x, length(y)) + rep(y, each = length(x))
cs <- SS[[1]]
for(i in 2:length(SS)) {
cs <- f(cs, SS[[i]])
}
# Those cs that add up to 221
which ( cs == 221 ) 发布于 2016-02-23 13:49:29
Beyond the response from danas above you could use a fast expanding grid
library(data.table)
# data.table::CJ is a fast expand.grid
# get all value combinations
DT <- do.call(CJ, args = input_list) # get all combination
# See link - http://stackoverflow.com/questions/35418479/tracing-the-location-index-of-the-components-of-a-cumulative-sum-of-sequential-l发布于 2016-02-15 14:17:26
expand.grid是你的杀手。我的解决方案:
f <- function(x, y) rep(x, length(y)) + rep(y, each = length(x))
cs <- SS[[1]]
for(i in 2:length(SS)) {
cs <- f(cs, SS[[i]])
}https://stackoverflow.com/questions/35410799
复制相似问题