Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从列表中提取条目并将每个条目存储在矩阵中_R_List - Fatal编程技术网

R 从列表中提取条目并将每个条目存储在矩阵中

R 从列表中提取条目并将每个条目存储在矩阵中,r,list,R,List,好的,我有以下关于R的列表: seg_sites list[1000] List of length 1000 [[1]] double [40x162] 00000000000000000000000... [[2]] double [40x150] 11110000000000000000000... [[3]] double [40x160] 00000000111111110000000... [[4]]

好的,我有以下关于R的列表:

seg_sites      list[1000]       List of length 1000
 [[1]]       double [40x162]    00000000000000000000000...
 [[2]]       double [40x150]    11110000000000000000000...
 [[3]]       double [40x160]    00000000111111110000000...
 [[4]]       double [40x93]     00110000000000111100000...
 [[5]]       double [40x144]    00000110000000000110000...
  ...
这个名单上还有一千个条目。如您所见,我总是有相同数量的行,但每个条目的列数不同。我想做的是,将每个条目提取到一个矩阵中。例如,我想创建一个名为“output_1”的矩阵,其中包含[[1]]条目的信息,然后使用[[2]]条目的信息输出_2,依此类推

我尝试使用for循环来实现此目的:

# Loop to intialize the matrices where I want to store the different entries
for(i in 1:nrep) {
     output_single_i <- matrix(data = NA, nrow = 40, ncol = ncol(single[["seg_sites"]][[i]]))
}

# Loop to actually store the different entries in each matrix 
for(i in 1:nrep) {  
  output_single_i <- matrix(single$seg_sites[[i]], ncol = ncol(single[["seg_sites"]][[i]]))
}
#循环初始化要存储不同条目的矩阵
适用于(1:nrep中的i){

输出\u single\u i我们可以使用
assign
从矩阵列表创建矩阵数据。矩阵数据保存在全局环境中,您可以使用
ls()进行检查

另一种方法是使用
list2env()
函数

names(mylist) <- paste0( 'output', seq_along(mylist))
list2env( mylist, envir = .GlobalEnv)

names(mylist)如果我理解正确,当前形式的数据很容易操作。你为什么要麻烦将每个数据单独存储?是的,列表形式的数据确实很容易操作,应该这么说。但是,列表上的每个条目都是不同的模拟(群体遗传学中的一种情况)我想比较和计算每个模拟器的各种统计数据你确定你想用成千上万的对象来扰乱你的工作空间,这些对象被称为
object\u 1
object\u 2
,等等?@Uwe是的,我知道它会扰乱工作空间,我可能会以另一种方式结束工作,但现在我只想这样做o找出如何做到这一点…只是为了了解it@Joãcaravalho您可以保持列表的原样,循环浏览此列表并从矩阵数据中提取摘要统计信息
感谢您的输入!但是,至少在我的案例中,这两种解决方案只创建了一个与我最初创建的列表完全相同的列表
fortunes::fortune(236)
:应该使用赋值函数的人只有那些完全理解为什么永远不应该使用赋值函数的人。-Greg Snow R-help(2009年7月)我看到了全球环境中的矩阵列表。请使用
ls()
@Uwe检查。如果可能,请提供Rhelp文章的链接。谢谢您:
names(mylist) <- paste0( 'output', seq_along(mylist))
list2env( mylist, envir = .GlobalEnv)
mylist <- list(matrix(1:10, ncol = 5), matrix(1:10, ncol = 2))
    str(mylist)
    # List of 2
    # $ : int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
    # $ : int [1:5, 1:2] 1 2 3 4 5 6 7 8 9 10