I have a list of matrix/vector and would like to apply R functions and store the output as a list in a sequence of numbers. As am example: I have a vector x and would like to calcualte min,max,median,quantile and store it in a list every iteration,
我有一个矩阵/向量列表,并希望应用R函数并将输出存储为数字序列中的列表。作为示例:我有一个向量x,并希望计算min,max,median,quantile并在每次迭代时将其存储在列表中,
outlist <- vector("list", 10)
for(p in seq(from=5, to=50, by=5)) {
x <- c(rnorm(20))
min <- min(x)
max <- max(x)
median <- median(x)
quant <- quantile(x)
outlist[[p]] <- list(c(min=min,max=max,median=median,quant=quant))
print(p)
}
The code above works fine, except that the output and has lots of "null" values . See below for the output from the program above, notice that 1 thru 4 is null and only in 5th iteration we start get the list output. This repeats for all the iteration. Can you please let me know how I should avoid null values in the output.
上面的代码工作正常,除了输出和有很多“null”值。请参阅下面有关上述程序的输出,请注意1到4为空,仅在第5次迭代中我们开始获取列表输出。这重复所有迭代。能告诉我如何避免输出中的空值。
Below are my questions. How to ensure all the null values are not present and only sequence numbers (p=5,10,15,...,50) in the iteration are present.
以下是我的问题。如何确保不存在所有空值,并且仅存在迭代中的序列号(p = 5,10,15,...,50)。
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
[[4]]
NULL
[[5]]
[[5]][[1]]
min max median quant.0% quant.25% quant.50% quant.75% quant.100%
-2.1058435 2.8461767 -0.5227028 -2.1058435 -1.1500056 -0.5227028 0.1645680 2.8461767
In addition, how to create a matrix(vector) output for list with row name as the sequence example ?
另外,如何为行列名作为序列示例创建列表的矩阵(向量)输出?
min max median quant.0% quant.25% quant.50% quant.75% quant.100%
5 -2.1058435 2.8461767 -0.5227028 -2.1058435 -1.1500056 -0.5227028 0.1645680 2.8461767
10 -0.7709128 2.2872216 0.1808006 -0.7709128 -0.1501660 0.1808006 0.8250730 2.2872216
15 -2.3748713 2.0360717 0.2638579 -2.3748713 -0.2631081 0.2638579 0.6446657 2.0360717
and so on ...
Thanks
谢谢
1 个解决方案
#1
1
You can build the list using the foreach
package. The foreach
function will return you a list and you can assign its names afterwards.
您可以使用foreach包构建列表。 foreach函数将返回一个列表,然后您可以指定其名称。
library(foreach)
vectorNames <- seq(from=5, to=50, by=5)
outlist <- foreach(p = vectorNames) %do% {
x <- c(rnorm(20))
min <- min(x)
max <- max(x)
median <- median(x)
quant <- quantile(x)
return(c(min=min,max=max,median=median,quant=quant))
}
names(outlist) <- vectorNames
Then, you can create the matrix using do.call
and rbind
.
然后,您可以使用do.call和rbind创建矩阵。
outmatrix <- do.call(rbind, outlist)
outmatrix
#1
1
You can build the list using the foreach
package. The foreach
function will return you a list and you can assign its names afterwards.
您可以使用foreach包构建列表。 foreach函数将返回一个列表,然后您可以指定其名称。
library(foreach)
vectorNames <- seq(from=5, to=50, by=5)
outlist <- foreach(p = vectorNames) %do% {
x <- c(rnorm(20))
min <- min(x)
max <- max(x)
median <- median(x)
quant <- quantile(x)
return(c(min=min,max=max,median=median,quant=quant))
}
names(outlist) <- vectorNames
Then, you can create the matrix using do.call
and rbind
.
然后,您可以使用do.call和rbind创建矩阵。
outmatrix <- do.call(rbind, outlist)
outmatrix