Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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
返回奇数结果的rbind()_R - Fatal编程技术网

返回奇数结果的rbind()

返回奇数结果的rbind(),r,R,这一切迹象都表明这是一件非常愚蠢的事情,我会后悔在一个公共论坛上问这个问题,但我现在已经在这件事上难倒了一些人,所以这就是生活 我正在运行以下代码块,但没有得到预期的结果: zz <- list(a=list('a', 'b', 'c', 'd'), b=list('f', 'g', '2', '1'), c=list('t', 'w', 'x', '6')) padMat <- do.call('cbind', zz) headMat <- matrix

这一切迹象都表明这是一件非常愚蠢的事情,我会后悔在一个公共论坛上问这个问题,但我现在已经在这件事上难倒了一些人,所以这就是生活

我正在运行以下代码块,但没有得到预期的结果:

zz <- list(a=list('a', 'b', 'c', 'd'), b=list('f', 'g', '2', '1'),
           c=list('t', 'w', 'x', '6'))
padMat <- do.call('cbind', zz)
headMat <- matrix(c(colnames(padMat), rep('foo', ncol(padMat))), nrow=2, byrow=TRUE)
rbind(headMat, padMat)
相反,我得到的是:

a    b    c
a    f    t 
b    g    w
c    2    x
d    1    6
NULL NULL NULL
它似乎在逐行填充rbind的上半部分,然后在末尾添加一行空值

几点注意:

  • 只要床头垫是一排的,它就可以工作

  • 为了再次检查,我还去掉了padMat的dimname,这并没有影响任何事情

  • 另一个想法是,它与byrow=TRUE有关,但是如果你去掉它,同样的行为也会发生


padMat
是一个列表(带有dim属性),而不是您通常认为的矩阵

> padMat <- do.call('cbind', zz)
> str(padMat)
List of 12
 $ : chr "a"
 $ : chr "b"
 $ : chr "c"
 $ : chr "d"
 $ : chr "f"
 $ : chr "g"
 $ : chr "2"
 $ : chr "1"
 $ : chr "t"
 $ : chr "w"
 $ : chr "x"
 $ : chr "6"
 - attr(*, "dim")= int [1:2] 4 3
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "a" "b" "c"
>padMat str(padMat)
12人名单
$:chr“a”
$:chr“b”
$:chr“c”
$:chr“d”
$:chr“f”
$:chr“g”
$:chr“2”
$:chr“1”
$:chr“t”
$:chr“w”
$:chr“x”
$:chr“6”
-属性(*,“dim”)=int[1:2]4 3
-属性(*,“dimnames”)=2个列表
..$:空
..$:chr[1:3]“a”“b”“c”
我猜你想要的是:

> padMat <- do.call(cbind,lapply(zz,c,recursive=TRUE))
> str(padMat)
 chr [1:4, 1:3] "a" "b" "c" "d" "f" "g" "2" "1" "t" "w" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "a" "b" "c"
>padMat str(padMat)
chr[1:4,1:3]“a”“b”“c”“d”“f”“g”“2”“1”“t”“w”。。。
-属性(*,“dimnames”)=2个列表
..$:空
..$:chr[1:3]“a”“b”“c”

这里的教训是,“
str
是你的朋友。”:)

问题似乎源于
padMat
是一个奇怪的矩阵。R报告是包含12个维度的列表:

R> str(padMat)
List of 12
 $ : chr "a"
 $ : chr "b"
 $ : chr "c"
 $ : chr "d"
 $ : chr "f"
 $ : chr "g"
 $ : chr "2"
 $ : chr "1"
 $ : chr "t"
 $ : chr "w"
 $ : chr "x"
 $ : chr "6"
 - attr(*, "dim")= int [1:2] 4 3
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "a" "b" "c"
这似乎是问题的根源,因为矩阵的重铸是有效的:

R> rbind(headMat, matrix(unlist(padMat), ncol = 3))
     [,1]  [,2]  [,3] 
[1,] "a"   "b"   "c"  
[2,] "foo" "foo" "foo"
[3,] "a"   "f"   "t"  
[4,] "b"   "g"   "w"  
[5,] "c"   "2"   "x"  
[6,] "d"   "1"   "6"

其他人正确地指出,
padMat
具有模式
list
,如果您查看rbind和cbind的文档,这是不好的:

In the default method, all the vectors/matrices must be atomic (see vector) or lists.
这就是为什么
do.call
有效,因为
zz
的元素本身就是列表。如果将
zz
的定义更改为以下内容:

zz <- list(a=c('a', 'b', 'c', 'd'), b=c('f', 'g', '2', '1'),
       c=c('t', 'w', 'x', '6'))

zz糟糕,继续按回车键,不要按住shift键。类(padMat)返回'matrix'Ah right(我看到您刚刚更新了相同的东西)。is.matrix是真的,但is.list也是真的。有趣。我以前从未(故意)遇到过这种情况。@geoffjentry read
?is.matrix
,请注意,
is.matrix()
如果应用于具有dim属性的向量,则返回TRUE。列表是一个向量,您的列表具有dim属性。因此,报告。但是看看
str(padMat)
…对,我现在明白了。刚刚在?cbind中看到你可以有一个列表矩阵,但没有意识到。@geoffjentry:请看@Gavin的评论。:)我将编辑
padMat
是一个带有dim属性的列表(这是一个向量)。对。在这种情况下,子列表必须是列表,示例中的VAL只是简化了。结果还表明,整个事情并不是正确的处理方式,但这个特殊的问题已经在办公室变成了一个脑筋急转弯
zz <- list(a=c('a', 'b', 'c', 'd'), b=c('f', 'g', '2', '1'),
       c=c('t', 'w', 'x', '6'))
The type of a matrix result determined from the highest type of any of the inputs 
 in the hierarchy raw < logical < integer < real < complex < character < list .