无法将 tibble 转换为 R 中的数组?
Cannot convert tibble to array in R?
我试图将一个非常简单的 1x4 tibble 转换为数组:
library(tidyverse)
temp <- tibble(x=0,y=1,z=1,w=1)
array(temp)
它给我以下错误信息:
Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) :
zero-length inputs cannot be mixed with those of non-zero length
In addition: Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
在数组函数中,似乎 dim(data) <- dim
部分是问题...我确实找到了一个解决方案,即将 tibble 转换为数据帧:
array(as.data.frame(temp))
但我不太清楚为什么我必须经过一个额外的步骤。有人可以告诉我我错过了什么吗?
从 tibble
版本 1.4.2 开始,无法再重现此行为。为了以后的参考,我留下以下记录:
> library(tidyverse)
> versions::installed.versions("tibble")
[1] "1.4.2"
> temp <- tibble(x=0,y=1,z=1,w=1)
> array(temp)
# A tibble: 1 x 4
<dbl> <dbl> <dbl> <dbl>
1 0. 1. 1. 1.
> array(as.data.frame(temp))
1 0 1 1 1
> simplify2array(temp)
x y z w
0 1 1 1
> dim(array(temp))
[1] 1 4
> dim(as.data.frame(array(temp)))
[1] 1 4
> dim(simplify2array(temp))
NULL
> class(array(temp))
[1] "tbl_df" "tbl" "data.frame"
> class(as.data.frame(array(temp)))
[1] "data.frame"
> class(simplify2array(temp))
[1] "numeric"
> as.array(temp)
Error in `dimnames<-.data.frame`(`*tmp*`, value = list(n)) :
invalid 'dimnames' given for data frame
我可以使用 simplify2array
创建 3D 数组,如下所示,但不幸的是,我不太记得我问这个问题时的目的。
simplify2array(by(temp, temp$x, as.matrix))
我试图将一个非常简单的 1x4 tibble 转换为数组:
library(tidyverse)
temp <- tibble(x=0,y=1,z=1,w=1)
array(temp)
它给我以下错误信息:
Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) : zero-length inputs cannot be mixed with those of non-zero length In addition: Warning messages: 1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL' 2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
在数组函数中,似乎 dim(data) <- dim
部分是问题...我确实找到了一个解决方案,即将 tibble 转换为数据帧:
array(as.data.frame(temp))
但我不太清楚为什么我必须经过一个额外的步骤。有人可以告诉我我错过了什么吗?
从 tibble
版本 1.4.2 开始,无法再重现此行为。为了以后的参考,我留下以下记录:
> library(tidyverse)
> versions::installed.versions("tibble")
[1] "1.4.2"
> temp <- tibble(x=0,y=1,z=1,w=1)
> array(temp)
# A tibble: 1 x 4
<dbl> <dbl> <dbl> <dbl>
1 0. 1. 1. 1.
> array(as.data.frame(temp))
1 0 1 1 1
> simplify2array(temp)
x y z w
0 1 1 1
> dim(array(temp))
[1] 1 4
> dim(as.data.frame(array(temp)))
[1] 1 4
> dim(simplify2array(temp))
NULL
> class(array(temp))
[1] "tbl_df" "tbl" "data.frame"
> class(as.data.frame(array(temp)))
[1] "data.frame"
> class(simplify2array(temp))
[1] "numeric"
> as.array(temp)
Error in `dimnames<-.data.frame`(`*tmp*`, value = list(n)) :
invalid 'dimnames' given for data frame
我可以使用 simplify2array
创建 3D 数组,如下所示,但不幸的是,我不太记得我问这个问题时的目的。
simplify2array(by(temp, temp$x, as.matrix))