如何根据百分比计算均值和标准差
How to calculate mean and standard deviation based on percentage
这是我的数据:
df1 <- read.table(text = " Group L1 L2 L3 L4 l5
Q 0% 10% 0% 70% 20%
K 20% 20% 20% 10% 30%", header = TRUE)
我想计算 Q 和 k 的 L1 乘以 1、L2 乘以 2、L3 乘以 3、L4 乘以 4 和 L5 乘以 5。
我想得到以下table:
Group L1 L2 L3 L4 l5 Mean SD
Q 0% 10% 0% 70% 20% 0.8 1.19
K 20% 20% 20% 10% 30% 0.62 0.51
注意:我有 100 行,这是我的样本的一部分,用于显示我的问题的目的。非常感谢您的帮助。
我们可以先从列中删除 "%"
符号。将 df1
乘以 1:5
并计算行向平均值和 sd。
df1[-1] <- lapply(df1[-1], function(x) as.numeric(sub('%', '', x)))
temp <- as.matrix(sweep(df1[-1], 2, seq_along(df1[-1]), `*`))
df1$Mean <- rowMeans(temp)/100
df1$Sds <- matrixStats::rowSds(temp)/100
#Or to keep it in base R
#df1$Sds <- apply(temp, 1, sd)/100
df1
# Group L1 L2 L3 L4 l5 Mean Sds
#1 Q 0 10 0 70 20 0.80 1.19164
#2 K 20 20 20 10 30 0.62 0.51186
数据
df1 <- structure(list(Group = structure(2:1, .Label = c("K", "Q"),class = "factor"),
L1 = structure(1:2, .Label = c("0%", "20%"), class = "factor"),
L2 = structure(1:2, .Label = c("10%", "20%"), class = "factor"),
L3 = structure(1:2, .Label = c("0%", "20%"), class = "factor"),
L4 = structure(2:1, .Label = c("10%", "70%"), class = "factor"),
l5 = structure(1:2, .Label = c("20%", "30%"), class = "factor")),
class = "data.frame", row.names = c(NA, -2L))
library(tidyverse)
df %>%
mutate(id = row_number()) %>%
pivot_longer(L1:L5) %>%
mutate(value = as.numeric(sub("%", "", value))/100 * 1:5) %>%
group_by(id) %>%
summarise(Mean = mean(value), SD = sd(value)) %>%
bind_cols(df, .)
# Group L1 L2 L3 L4 L5 id Mean SD
# 1 Q 0% 10% 0% 70% 20% 1 0.80 1.1916375
# 2 K 20% 20% 20% 10% 30% 2 0.62 0.5118594
数据
df <- structure(list(Group = structure(2:1, .Label = c("K", "Q"), class = "factor"),
L1 = structure(1:2, .Label = c("0%", "20%"), class = "factor"),
L2 = structure(1:2, .Label = c("10%", "20%"), class = "factor"),
L3 = structure(1:2, .Label = c("0%", "20%"), class = "factor"),
L4 = structure(2:1, .Label = c("10%", "70%"), class = "factor"),
L5 = structure(1:2, .Label = c("20%", "30%"), class = "factor")),
class = "data.frame", row.names = c(NA, -2L))
这是我的数据:
df1 <- read.table(text = " Group L1 L2 L3 L4 l5
Q 0% 10% 0% 70% 20%
K 20% 20% 20% 10% 30%", header = TRUE)
我想计算 Q 和 k 的 L1 乘以 1、L2 乘以 2、L3 乘以 3、L4 乘以 4 和 L5 乘以 5。
我想得到以下table:
Group L1 L2 L3 L4 l5 Mean SD
Q 0% 10% 0% 70% 20% 0.8 1.19
K 20% 20% 20% 10% 30% 0.62 0.51
注意:我有 100 行,这是我的样本的一部分,用于显示我的问题的目的。非常感谢您的帮助。
我们可以先从列中删除 "%"
符号。将 df1
乘以 1:5
并计算行向平均值和 sd。
df1[-1] <- lapply(df1[-1], function(x) as.numeric(sub('%', '', x)))
temp <- as.matrix(sweep(df1[-1], 2, seq_along(df1[-1]), `*`))
df1$Mean <- rowMeans(temp)/100
df1$Sds <- matrixStats::rowSds(temp)/100
#Or to keep it in base R
#df1$Sds <- apply(temp, 1, sd)/100
df1
# Group L1 L2 L3 L4 l5 Mean Sds
#1 Q 0 10 0 70 20 0.80 1.19164
#2 K 20 20 20 10 30 0.62 0.51186
数据
df1 <- structure(list(Group = structure(2:1, .Label = c("K", "Q"),class = "factor"),
L1 = structure(1:2, .Label = c("0%", "20%"), class = "factor"),
L2 = structure(1:2, .Label = c("10%", "20%"), class = "factor"),
L3 = structure(1:2, .Label = c("0%", "20%"), class = "factor"),
L4 = structure(2:1, .Label = c("10%", "70%"), class = "factor"),
l5 = structure(1:2, .Label = c("20%", "30%"), class = "factor")),
class = "data.frame", row.names = c(NA, -2L))
library(tidyverse)
df %>%
mutate(id = row_number()) %>%
pivot_longer(L1:L5) %>%
mutate(value = as.numeric(sub("%", "", value))/100 * 1:5) %>%
group_by(id) %>%
summarise(Mean = mean(value), SD = sd(value)) %>%
bind_cols(df, .)
# Group L1 L2 L3 L4 L5 id Mean SD
# 1 Q 0% 10% 0% 70% 20% 1 0.80 1.1916375
# 2 K 20% 20% 20% 10% 30% 2 0.62 0.5118594
数据
df <- structure(list(Group = structure(2:1, .Label = c("K", "Q"), class = "factor"),
L1 = structure(1:2, .Label = c("0%", "20%"), class = "factor"),
L2 = structure(1:2, .Label = c("10%", "20%"), class = "factor"),
L3 = structure(1:2, .Label = c("0%", "20%"), class = "factor"),
L4 = structure(2:1, .Label = c("10%", "70%"), class = "factor"),
L5 = structure(1:2, .Label = c("20%", "30%"), class = "factor")),
class = "data.frame", row.names = c(NA, -2L))