Number of significant digits in dplyr summarise

后端 未结 3 1372
孤街浪徒
孤街浪徒 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:37

    I think the simplest solution is the following:

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

提交回复
热议问题