如何在R中的数据框中仅对列中的某些行进行标准偏差?

时间:2015-04-09 00:48:42

标签: r dataframe partitioning

我有一个包含652行(特别是产品)和3个特定感兴趣列的数据框:stand_aov,stand_cr和cluster_labels10。我有兴趣在每个cluster_label10中找到stand_aov和stand_cr的标准差,然后将其导出到一个简单的数据框,该数据框只列出了由cluster_label10标准偏差的stand_aov和stand_cr。 652个产品中的每一个都属于cluster_labels10,并且所有产品都标记为1-10。

理想情况下,输出最终只包含3列(cluster_labels10 ID,每个群集标签的stand_aov的stdev,每个群集的stand_cr的stdev)和10行 - 每个cluster_labels10的1行。

举一个例子来说明第一行的样子:

cluster_labels10 stdev_stand_aov stdev_stand_cr
               1            .001           .001

3 个答案:

答案 0 :(得分:2)

使用aggregate的基础R解决方案:

set.seed(123)
df <- data.frame(
        cluster_labels10 = rep(c(1, 2, 3), each = 5),
        stand_aov = rnorm(15),
        stand_cr = rnorm(15)
)

aggregate(df[2:3], list(df$cluster_labels10), sd)
  Group.1 stand_aov  stand_cr
1       1 0.8110218 1.4110413
2       2 1.1634896 0.3445583
3       3 0.6394632 1.2619931

答案 1 :(得分:1)

也许您可以使用dplyr

require(dplyr)
set.seed(123)
DF <- data.frame(
  cluster_labels10 = rep(c(1, 2, 3), each = 5),
  stand_aov = rnorm(15),
  stand_cr = rnorm(15)
)


> DF
   cluster_labels10   stand_aov   stand_cr
1                 1 -0.56047565  1.7869131
2                 1 -0.23017749  0.4978505
3                 1  1.55870831 -1.9666172
4                 1  0.07050839  0.7013559
5                 1  0.12928774 -0.4727914
6                 2  1.71506499 -1.0678237
7                 2  0.46091621 -0.2179749
8                 2 -1.26506123 -1.0260044
9                 2 -0.68685285 -0.7288912
10                2 -0.44566197 -0.6250393
11                3  1.22408180 -1.6866933
12                3  0.35981383  0.8377870
13                3  0.40077145  0.1533731
14                3  0.11068272 -1.1381369
15                3 -0.55584113  1.2538149

DF %>% 
  group_by(cluster_labels10) %>% 
  summarise(x = sd(stand_aov), y = sd(stand_cr))

输出:

Source: local data frame [3 x 3]

  cluster_labels10         x         y
1                1 0.8110218 1.4110413
2                2 1.1634896 0.3445583
3                3 0.6394632 1.2619931

答案 2 :(得分:1)

使用by的另一个基础R 方法:

# generate random data
dat <- data.frame(cluster_labels10 = sample(1:10, size = 652, replace = TRUE), 
                  stand_aov = rnorm(n = 652), stand_cr = rnorm(n = 652))

使用by根据cluster_labels10

按观察组计算统计数据
sd.1 <- by(data = dat$stand_aov, INDICES = dat$cluster_labels10, FUN = sd)
sd.2 <- by(data = dat$stand_cr, INDICES = dat$cluster_labels10, FUN = sd)
final <- cbind(cluster_labels10 = as.numeric(names(sd.1)), 
               stdev_stand_aov = sd.1, stdev_stand_cr = sd.2)

结果

final

#    cluster_labels10 stdev_stand_aov stdev_stand_cr
# 1                 1       0.8785011      1.0402992
# 2                 2       1.0942536      1.3726442
# 3                 3       0.9294320      0.9795918
# 4                 4       1.1355244      1.1050766
# 5                 5       1.0023296      0.8770729
# 6                 6       1.1367627      0.9499932
# 7                 7       0.9796322      0.9257972
# 8                 8       0.9715574      1.0221725
# 9                 9       0.9044647      1.0052602
# 10               10       1.1215173      1.1609340