Reputation: 4298
I was trying to convert a very simple 1x4 tibble to an array:
library(tidyverse)
temp <- tibble(x=0,y=1,z=1,w=1)
array(temp)
It gives me the following error messages:
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'
Within the array function it seems like dim(data) <- dim
part is the problem... I did figure out a solution, which is to turn the tibble into a dataframe:
array(as.data.frame(temp))
But I'm not quite sure why I have to go through an extra step. Could somebody tell me what I'm missing?
Upvotes: 4
Views: 4282
Reputation: 4298
As of tibble
version 1.4.2, this behavior can no longer be reproduced. For future references, I leave the following record:
> 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
I could use simplify2array
to create 3D arrays such as follows, but unfortunately I do not quite remember what I was going for at the time when I asked this question.
simplify2array(by(temp, temp$x, as.matrix))
Upvotes: 4