簡體   English   中英

如何在R中按組同時計算均值,cv和標准差

[英]How to compute mean, cv and standard deviation simultaneously using by group in R

我想同時計算多個變量的均值,cv和標准差,然后將輸出分組(使用by語句)。 以下是我的數據的快照。

CLT             SOURCE          LD     BRX  

  Bt02Sm86(Lg)  GH801-2@ S7        1.5   8.6 
  Bt02Sm86(Lg)  GH801-2@ S7        1.3   8.9  
  Bt02Sm86(Lg)  GH801-2@ S7        1.8   8.7  
  Bt14Lg 26x36  GH811x810F1        1.4   9.6
  Bt14Lg 26x36  GH811x810F1        1.6  10.2   
  Bt03Sm86(Lg)  GH802-1@ S7        1.3   9.9
  Bt03Sm86(Lg)  GH802-1@ S7        1.2   8.9
  Bt03Sm86(Lg)  GH802-1@ S7        1.3   9.3   
  Bt18Lg 15x36  GH818x819F1        1.3   9.7 
  Bt18Lg 15x36  GH818x819F1        1.1   8.5 
  Bt18Lg 15x36  GH818x819F1        1.6   9.3   
  Bt19Lg 15x39  GH820x821F1        1.3   9.1
  Bt19Lg 15x39  GH820x821F1        1.5   9.3  

我希望我的輸出以以下方式顯示

 CLT            SOURCE          mean_LD  cv_LD  std_LD  mean_BRX  cv_ld std_brx

Bt02Sm86(Lg)    GH801-2@ S7      xx.xx    xx.xx  xx.xx   xx.xx    xx.xx  xx.xx
Bt14Lg 26x36  GH811x810F1        xx.xx    xx.xx  xx.xx   xx.xx    xx.xx  xx.xx
Bt03Sm86(Lg)  GH802-1@ S7        xx.xx    xx.xx  xx.xx   xx.xx    xx.xx  xx.xx
Bt18Lg 15x36  GH818x819F1        xx.xx    xx.xx  xx.xx   xx.xx    xx.xx  xx.xx 
Bt19Lg 15x39  GH820x821F1        xx.xx    xx.xx  xx.xx   xx.xx    xx.xx  xx.xx 

任何建議表示贊賞

嘗試

cv <- function(x) 100*( sd(x)/mean(x))
library(dplyr)
 df1 %>% 
     group_by(CLT, SOURCE) %>% 
     summarise_each(funs(mean, sd, cv))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM