jeudi 25 avril 2019

`dplyr::if_else()` compared to base R `ifelse()` - why rbindlist error?

This code block below utilizing dplyr::if_else() works without issue and produces the flextable shown.

library(tidyverse)
library(flextable)

# utilizing dplyr if_else
df1 <- tibble(col1 = c(5, 2), col2 = c(6, 4)) %>% 
  mutate(col3 = if_else(apply(.[, 1:2], 1, sum) > 10 & .[, 2] > 5, 
                        "True",
                        "False"))
df1 %>% flextable() %>% theme_zebra()

table

I first tried this with base R ifelse() and got the error shown below. The error doesn't seem to make any sense. Can somebody explain? My data frame doesn't have anywhere near 15 columns.

# utilizing base R ifelse
df2 <- tibble(col1 = c(5, 2), col2 = c(6, 4)) %>% 
  mutate(col3 = ifelse(apply(.[, 1:2], 1, sum) > 10 & .[, 2] > 5, 
                        "True",
                        "False"))
df2 %>% flextable() %>% theme_zebra()

# Error in rbindlist(x$content$data) : 
  # Item 5 has 15 columns, inconsistent with item 1 which has 14 columns. 
  # To fill missing columns use fill=TRUE.

Aucun commentaire:

Enregistrer un commentaire