2017-11-23 1 views
0

Ich hoffe, erstellen (Mittelwert ± SD) Form.
unten BeispielcodeWie man den Mittelwert + -sd data.frame erstellt?

a=c("type","A","B","C") 
b=c("a","a","b","b") 
c=c(22, 32, 23, 20) 
d=c(12,25,23,30) 
e=c(15,17,23,35) 
f=data.frame(b,c,d,e) 
colnames(f)=a 

neue Tabelle Komponente Mittelwert ± SD von Typ ist.
wie dieses ..

type A B C 
a mean ± sd .. mean ± sd 
b mean ± sd .. mean ± sd 

mir bitte helfen

Antwort

1

Wir können Gruppe von 'Typ' und summarise_all verwenden. Unter der Annahme, dass wir die mean und sd und nutzen Sie die zusammengefassten Spalten

library(dplyr) 
f %>% 
    group_by(type) %>% 
    summarise_all(funs(mean(.) + round(sd(.), 2), mean(.)- round(sd(.), 2))) 
# A tibble: 2 x 7 
# type `A_+` `B_+` `C_+` `A_-` `B_-` `C_-` 
# <fctr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 
#1  a 34.07 27.69 17.41 19.93 9.31 14.59 
#2  b 23.62 31.45 37.49 19.38 21.55 20.51 

hinzufügen möchten, und subtrahieren Wenn dies als character Klasse benötigt wird

f %>% 
    group_by(type) %>% 
    summarise_all(funs(paste(mean(.), round(sd(.), 2), sep=" ± "))) 
# A tibble: 2 x 4 
# type   A   B   C 
# <fctr>  <chr>  <chr>  <chr> 
#1  a 27 ± 7.07 18.5 ± 9.19 16 ± 1.41 
#2  b 21.5 ± 2.12 26.5 ± 4.95 29 ± 8.49 
+0

Dank aber, ich will wirklich Form " 20 ± 0,5 "für Papier. eine Zelle apperance "Mittelwert ± SD" –

+0

@SungminYang Mit zweiter Option aktualisiert. Bitte überprüfen Sie – akrun

+1

Oh! ich will es! Vielen Dank!!! :) –

Verwandte Themen