0

サンプルの調査シートがあります。人口統計のようなもの。country (factor)列の 1 つは別の列ですannual incomeここで、各国の平均を計算し、 new data.framewithcountryおよび対応するmeanに保存する必要があります。シンプルなはずなのに迷ってしまいます。データは、次の図のようなものです。

Country  Income($) Education ... ... ...
1. USA    90000      Phd
2. UK     94000      Undergrad
3. USA    94000      Highschool
4. UK     87000      Phd
5. Russia 77000      Undergrad
6. Norway 60000      Masters
7. Korea  90000      Phd
8. USA    110000     Masters
.
.

次のような最終結果が必要です。

USA   UK    Russia ...
98000 90000 75000

ありがとうございました。

4

1 に答える 1

5

データ例:

dat <- read.table(text="Country  Income Education 
 USA    90000      Phd
 UK     94000      Undergrad
 USA    94000      Highschool
 UK     87000      Phd
 Russia 77000      Undergrad
 Norway 60000      Masters
 Korea  90000      Phd
 USA    110000     Masters",header=TRUE)

あなたがやりたいことをしてくださいplyr

データが呼び出された場合dat

library(plyr)
newdf <- ddply(dat, .(Country), function(x) Countrymean = mean(x$Income))

# newdf <- ddply(dat, .(Country), function(x) data.frame(Income = mean(x$Income)))

および集計:

 newdf <- aggregate(Income ~ Country, data = dat, FUN = mean)

最後に表示する出力については、多分tapply

tapply(dat$Income, dat$Country, mean)
于 2013-02-16T19:33:06.000 に答える