Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior of flatten_dfr #377

Closed
expersso opened this issue Aug 23, 2017 · 2 comments
Closed

Unexpected behavior of flatten_dfr #377

expersso opened this issue Aug 23, 2017 · 2 comments

Comments

@expersso
Copy link

Consider the following:

> l <- list(c(1, 2), c(3, 4))
> l
[[1]]
[1] 1 2

[[2]]
[1] 3 4

> do.call(rbind, l)
     [,1] [,2]
[1,]    1    2
[2,]    3    4
> flatten_dfr(l)
Error in bind_rows_(x, .id) : 'getCharCE' must be called on a CHARSXP

I would expect flatten_dfr(l) == do.call(rbind, l).

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.7.2        purrr_0.2.3.9000   readr_1.1.1        tidyr_0.6.3        tibble_1.3.3       ggplot2_2.2.1.9000 tidyverse_1.1.1  
@elong0527
Copy link

And the function will report error if the input is a list of matrix.

library(tidyverse)

tmp <- data.frame(xx = rnorm(10), yy = rnorm(10), grp = rbinom(10, size = 1, prob = 0.5))
foo <- ~  coef(summary(lm(xx~yy, data = .)))

x <- tmp %>% 
     split(.$grp) %>%
     map(foo) 

x %>% flatten_dfr()
Error in bind_rows_(x, .id) : Argument 1 must have names

@yutannihilation
Copy link
Member

data.frame and matrix are different things in terms of data structure, so I feel it is natural that flatten_dfr() throws error because it accepts data.frame, not matrix.

is(do.call(rbind, list(c(1, 2), c(3, 4))))
#> [1] "matrix"    "array"     "structure" "vector"

@elong0527
In your case, you can convert it to data.frame before using flatten_dfr() theoretically. But because of #374, you cannot do this for now.

x %>% map(as.data.frame) %>% flatten_dfr(.id = "id")
#> # A tibble: 2 x 5
#>      id    Estimate `Std. Error`  `t value` `Pr(>|t|)`
#>   <chr>       <dbl>        <dbl>      <dbl>      <dbl>
#> 1     1  0.09920347    0.5399731  0.1837193  0.8631710
#> 2     1 -0.14516082    0.6164339 -0.2354848  0.8253975

map_dfr() (or some function in broom package?) may help.

x %>% map_dfr(as.data.frame, .id = "id")
#>   id    Estimate Std. Error    t value  Pr(>|t|)
#> 1  0 -1.14290763  0.4655154 -2.4551444 0.1334761
#> 2  0 -0.47259892  0.3970154 -1.1903793 0.3560344
#> 3  1  0.09920347  0.5399731  0.1837193 0.8631710
#> 4  1 -0.14516082  0.6164339 -0.2354848 0.8253975

@hadley hadley closed this as completed Feb 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants