0

我只是想做一件基本的事情,但我似乎无法弄清楚问题是什么,而且我在这里找不到与我的问题完全一样的答案。如果有人已经知道其他地方的答案,请随时链接。

我有一个生成向量的模拟,并且我已经设置了我的模拟,以便它抓取生成向量并使其成为另一个向量的元素。在我多次运行模拟后,我想将向量的向量变成一个矩阵,但控制台输出总是这样:

 >   agx1     
[1,] Numeric,7
[2,] Numeric,7

我的模拟几乎做了以下事情:

agnx1 = c()

#some stuff happens

agnx1[i] = x1

#iteration number two takes place

agnx1[i+1] = x1

#etc..

#Now say I have

agx1[1] = c(0.796399, 0.865736, 0.885808, 0.896138, 0.896138, 0.850385, NA)

#and

agx1[2] = c(0.796399, 0.856540, 0.881432, 0.900808, 0.900808, 0.857664, NA)

#and therefore, agx1 is a vector of vectors. But whenever I try something like..

cagx1 = cbind(agx1[1:2])

#or

cagx1 = as.matrix(agx1)

# I just get:   

 [,1]     
[1,] Numeric,7
[2,] Numeric,7

任何的意见都将会有帮助。

4

2 回答 2

2

如果不查看所有数据很难判断,但可能agx1是一个列表。尝试使用do.call.

do.call(cbind, agx1)

编辑

Base Rcbind没有在列表上工作的功能。考虑一下:

cbind(agx1[[1]],agx1[[2]])

之所以有效,是因为您已取消列出第一个和第二个元素并将它们作为向量传递给cbind.

您可以使用do.call. help(do.call)说:

描述

do.call 从名称或函数以及要传递给它的参数列表构造并执行函数调用。

因此,通过从参数列表构造函数调用,do.call可以帮助您调用cbind(agx1[[1]], agx1[[2]], ...等等,直到列表末尾。cbindagx1

于 2020-03-31T01:59:20.463 回答
0

假设agx1是这样的:

agx1 <- list(c(0.796399, 0.865736, 0.885808, 0.896138, 0.896138, 0.850385, NA),
             c(0.796399, 0.856540, 0.881432, 0.900808, 0.900808, 0.857664, NA))

你可以使用dplyr::bind_cols

dplyr::bind_cols(agx1)

#     V1     V2
#   <dbl>  <dbl>
#1  0.796  0.796
#2  0.866  0.857
#3  0.886  0.881
#4  0.896  0.901
#5  0.896  0.901
#6  0.850  0.858
#7     NA     NA    
于 2020-03-31T02:12:25.433 回答