Home > Blockchain >  Using R nycflights tidyverse, rouding output
Using R nycflights tidyverse, rouding output

Time:02-28

trying to round output to equal this:

1340.24, has to show 2 figures after decimal
1      AA 1340.24
2      EV 562.99
3      FL 664.83

my output shows

1 AA        1340.2 
2 EV         562.99
3 FL         664.83
Q1 <- -filter(flights, carrier=="AA" | carrier=="EV" | carrier=="FL" ) %>% group by(carrier)%>% summarise(mean_dest=round((mean(distance)),2))

CodePudding user response:

How about rounding to two decimal places and setting pillar.sigfig?

library(nycflights13)
Q1 <- (flights 
  %>% filter(carrier %in% c("AA", "EV", "FL"))
  %>% group_by(carrier)
  %>% summarise(across(distance, mean))
)
options(pillar.sigfig = 10)
print(Q1 %>% mutate(across(distance, round, 2)))

Results:

# A tibble: 3 × 2
  carrier distance
  <chr>      <dbl>
1 AA       1340.24
2 EV        562.99
3 FL        664.83

Although as an answer to Controlling decimal places displayed in a tibble. Understanding what pillar.sigfig does points out, the decimal places may still not be displayed properly if there are trailing zeros ...

print(Q1 %>% mutate(across(distance, format, digits=1, nsmall =2)))

also works, although it leaves you with quotation marks ...

  • Related