返回列表显示单个NULL维度

时间:2014-09-21 06:17:17

标签: r function return return-value

我有两个数组:A和B,想要从函数中返回它们。我使用了list命令:

list1 <- list(A,B)
return(list1)

在原始功能中,我这样做是为了再次拆分列表:

A <- list1[1]
B <- list1[2]

这很好用,除了在原来的功能中我尝试在其他数学中使用A时,它告诉我尺寸不匹配。事实证明,因为我使用了list,所以两个数组现在都有一个NULL维度。 list1本身具有NULL维度。

关于如何从R中的函数返回两个数组并且仍然保留数组维度的任何想法?

感谢。

编辑1: 我正在制作一个用于平分k均值和A =&gt;的代码。 2×4质心坐标数组,b =&gt; 50x2 0/1值的数组。 输出dput(list1):(不知道这意味着什么)

list(structure(list(A = c(5.25555555555556, 4.71304347826087), 
B = c(3.67037037037037, 3.12173913043478), C = c(1.5037037037037, 
1.41739130434783), D = c(0.288888888888889, 0.191304347826087
)), .Names = c("A", "B", "C", "D"), row.names = 1:2, class = "data.frame"), 
structure(c(1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 
1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 
1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
1, 1, 0, 0, 1, 0, 1, 0, 0), .Dim = c(50L, 2L)))

1 个答案:

答案 0 :(得分:3)

对于R中的列表,您需要使用双方括号运算符[[以获取列表元素的内容。使用单个括号[返回列表。这是一个例子:

A <- matrix(rnorm(100), ncol=10)
B <- matrix(rnorm(100), ncol=10)
list1 <- list(A,B)
A_list <- list1[1]
A_vector <- list1[[1]]
is.list(A_list)
dim(A_list)
identical(A, A_vector)
dim(A_vector)