1

我得到一个 csv 文件,其中包含如下数据:

> "value","name"
> 403.375,"C1"
> 409.625,"C1"
> 300.5,"C11"
> 321.125,"C11"
> 740.25,"C2"
> 718.875,"C2"
> 303.25,"Q"
> 323,"Q"
> 146.875,"H1"
> 171.75,"H1"
> 210.875,"H2"
> 234.625,"H2"
> 1640,"CQ1"
> 1742.5,"CQ1"
> 2319.875,"CH1"
> 2449,"CH1"
> 1227.25,"CH2"
> 1157.375,"CH2"
> 781,"CQ1.1"
> 715.125,"CQ1.1"
> 713.125,"CH1.1"
> 670.25,"CH1.1"

如何按名称获取值的平均值,如下所示:

> "mean","name"
> 406.5,"C1"
> 310.8125,"C11"
> ... ...

并让条形图在 R 中按名称显示平均标签?

4

1 回答 1

7

数据 <- read.csv('data.csv')

数据

      value  name
1   403.375    C1
2   409.625    C1
3   300.500   C11
4   321.125   C11
5   740.250    C2
6   718.875    C2
7   303.250     Q
8   323.000     Q
9   146.875    H1
10  171.750    H1
11  210.875    H2
12  234.625    H2
13 1640.000   CQ1
14 1742.500   CQ1
15 2319.875   CH1
16 2449.000   CH1
17 1227.250   CH2
18 1157.375   CH2
19  781.000 CQ1.1
20  715.125 CQ1.1
21  713.125 CH1.1
22  670.250 CH1.1
> tapply(data$value, data$name, mean)
       C1       C11        C2       CH1     CH1.1       CH2       CQ1     CQ1.1 
 406.5000  310.8125  729.5625 2384.4375  691.6875 1192.3125 1691.2500  748.0625 
       H1        H2         Q 
 159.3125  222.7500  313.1250
于 2013-05-31T13:32:59.297 回答