Мы могли бы сначала удалить знак "%"
из столбцов. Умножьте df1
на 1:5
и вычислите среднее значение по строкам и сд.
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))