在R中计算Group By中的Mean时重复输入

时间:2015-11-18 01:06:24

标签: r dataframe

我有以下数据框mydata

PC1             sector
0.0002187305    Utilities
0.0002369591    Financials
0.0003339332    Health Care
0.0006809497    Financials
0.0004811478    Consumer Staples
0.0015884347    Consumer Staples
0.0004643183    Consumer Discretionary
0.0001093301    Energy
0.0002807079    Consumer Staples
0.0003112860    Health Care
0.0002418978    Information Technology

我正在尝试获取按扇区类别分组的PC1列的平均值。 我尝试了以下代码:

尝试1

data.plot2 <- ddply(mydata,~sector, summarise, mean=mean(as.numeric(as.character(PC1))))

两者都给我以下输出:

Consumer Discretionary  0.0004643183
Consumer Staples        0.0015884347
Consumer Staples        0.0003809279
Energy                  0.0001093301
Financials              0.0004589544
Health Care             0.0003226096
Information Technology  0.0002418978
Utilities               0.0002187305

str(mydata)给了我以下内容:

'data.frame':   11 obs. of  2 variables:
 $ PC1   : num  0.000219 0.000237 0.000334 0.000681 0.000481 ...
 $ sector: Factor w/ 21 levels "Consumer ...

有人可以突出我做错了吗?

0 个答案:

没有答案