Number of significant digits in dplyr summarise

后端 未结 3 1370
孤街浪徒
孤街浪徒 2021-01-08 00:24

I am having trouble getting the desired number of decimal places from summarise. Here is a simple example:

test2  <- data.frame(c(\"a\",\"a\",\"b\",\"b\")         


        
3条回答
  •  孤城傲影
    2021-01-08 00:54

    This is one solution-

    test2  <- data.frame(c("a", "a", "b", "b"), c(245, 246, 247, 248))
    library(dplyr)
    colnames(test2)  <- c("V1", "V2")
    group_by(test2, V1) %>% 
      dplyr::summarise(mean(V2)) %>% 
      dplyr::mutate_if(is.numeric, format, 1)
    #> # A tibble: 2 x 2
    #>   V1    `mean(V2)`
    #>         
    #> 1 a     245.5     
    #> 2 b     247.5
    

    Created on 2018-01-20 by the reprex package (v0.1.1.9000).

提交回复
热议问题