BWo*_*olk 5 r paste mean standard-deviation
我想创建一个数据框架,其中包含包含工具的几个不同的列,然后在方括号中显示sd。举个例子:
df <- iris
mean <- aggregate(df[,1:4], list(iris$Species), mean)
sd <- aggregate(df[,1:4], list(iris$Species), sd)
view(mean)
Group.1 Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 5.006 3.428 1.462 0.246
2 versicolor 5.936 2.770 4.260 1.326
3 virginica 6.588 2.974 5.552 2.026
view(sd)
Group.1 Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 0.3524897 0.3790644 0.1736640 0.1053856
2 versicolor 0.5161711 0.3137983 0.4699110 0.1977527
3 virginica 0.6358796 0.3224966 0.5518947 0.2746501
Run Code Online (Sandbox Code Playgroud)
现在我想要这样的东西:
Group.1 Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 5.0 (0.35) 3.4 (0.38) 1.5 (0.17) 0.2 (0.11)
2 versicolor 5.9 (0.52) 2.8 (0.31) 4.3 (0.47) 1.3 (0.20)
3 virginica 6.6 (0.64) 3.0 (0.32) 5.6 (0.55) 2.0 (0.27)
Run Code Online (Sandbox Code Playgroud)
我认为应该有一种使用该paste功能的方法,但我不知道如何使用。
我们可以直接将数据转换为matrix并应用paste
dfN <- mean
dfN[-1] <- paste0(round(as.matrix(mean[-1]), 1), " (",
round(as.matrix(sd[-1]), 2), ")")
Run Code Online (Sandbox Code Playgroud)
此外,这可以一步完成,而不是创建多个数据集
library(dplyr)
library(stringr)
df %>%
group_by(Species) %>%
summarise_all(list(~ str_c(round(mean(.), 2), " (", round(sd(.), 2), ")")))
# A tibble: 3 x 5
# Species Sepal.Length Sepal.Width Petal.Length Petal.Width
# <fct> <chr> <chr> <chr> <chr>
#1 setosa 5.01 (0.35) 3.43 (0.38) 1.46 (0.17) 0.25 (0.11)
#2 versicolor 5.94 (0.52) 2.77 (0.31) 4.26 (0.47) 1.33 (0.2)
#3 virginica 6.59 (0.64) 2.97 (0.32) 5.55 (0.55) 2.03 (0.27)
Run Code Online (Sandbox Code Playgroud)