Search code examples
rsumflextable

How to remove/change decimal sum value in flextable


I managed to create a flextable based on the below code. The data that is being used in opleiding1 through 4 looks a bit like this: 0, 0, 1, 7, 3, 7, 0

t7 <- data.frame(df$opleiding.1.[df$startvraag=="Ja"],
                 df$opleiding.2.[df$startvraag=="Ja"],
                 df$opleiding.3.[df$startvraag=="Ja"],
                 df$opleiding.4.[df$startvraag=="Ja"]
)

t7 <- data.frame(aantal=(apply(t7, 2, sum))) %>% round(digits = 0)
rownames(t7) <- c("hbo, universitair", "mbo, vwo, havo", "vmbo/mavo", "onbekend")
t7

Creating a flextable with the following code:

t7 <- xtable(t7)

ft7 <- t7 %>% xtable_to_flextable() %>% 
  fontsize(part = "header", size = 20) %>%
  fontsize(part = "body", size = 18) %>%
  align_text_col(align = "left") %>% 
  align_nottext_col(align = "center") %>%
  width(width = 2.3) %>%
  width(j = 1, width = 2.0) %>%
  height_all(height = .5) %>%
  border_inner(border = std_border) %>% 
  font(fontname = "Open Sans") %>%
  color(part = "header", color = d) %>% 
ft7

This gives me a flextable which is just as I want it except for the decimal which are shown here. I've looked for possible solutions but nothing seems to work. It looks like the decimal is added when converting the table to an flextable. When I run the previous code (mentions on top) it does not return with any decimals as shown here. Hope someone can help me sort this.


Solution

  • I think that is because your aantal column's type is double, meaning it is holding real values. You can change it's type to integer as you create the data.frame.

    Try this

    t7 <- data.frame(aantal=(apply(t7, 2, sum))) %>% 
        round(digits = 0) %>%
        mutate(aantal = as.integer(aantal))