1

5 か国の 10 年間のパフォーマンス スコアがあるとします。一部の国のパフォーマンスが特定の年に大幅に変化したことはご存知でしょう。ここで、それらがプラスに変化したか、マイナスに変化したかを知りたいと考えています。私を悩ませているのは、この最後のステップです。

サンプルデータ:

mydata<-1:3
mydata<-expand.grid(
country=c('A', 'B', 'C', 'D', 'E'),
year=c('1980','1981','1982','1983','1984','1985','1986','1987','1988','1989'))
mydata$score=sapply(runif(50,0,2), function(x) {round(x,4)})
library(reshape)
mydata<-reshape(mydata, v.names="score", idvar="year", timevar="country", direction="wide")

変更の識別:

score.cols <- grep("score", colnames(mydata), value=TRUE)
period.cols <- gsub("score", "period", score.cols)
compute.period <- function(x)as.integer(c(NA, abs(diff(x)) >= 0.5))
cbind(mydata, `names<-`(lapply(mydata[score.cols], compute.period), period.cols))

> cbind(mydata, `names<-`(lapply(mydata[score.cols], compute.period), period.cols))
   year score.A score.B score.C score.D score.E period.A period.B period.C period.D period.E
1  1980  0.4029  0.3308  1.0432  0.7405  0.7254       NA       NA       NA       NA       NA
6  1981  1.7577  0.5479  1.4437  1.3996  0.8454        1        0        0        1        0
11 1982  1.9603  0.5404  1.2687  1.4317  0.0203        0        0        0        0        1
16 1983  0.5509  1.5834  1.3954  0.4935  0.4994        1        1        0        1        0
21 1984  1.9672  1.0628  1.8436  0.4327  0.0144        1        1        0        0        0
26 1985  1.6799  1.5873  0.5898  0.9553  1.3475        0        1        1        1        1
31 1986  1.2918  1.7049  0.3448  0.1841  0.9270        0        0        0        1        0
36 1987  0.1719  0.3297  0.6386  0.4075  1.8494        1        1        0        0        1
41 1988  0.7123  1.2378  0.9220  0.3278  1.5888        1        1        0        0        0
46 1989  0.2998  0.4418  1.0640  1.1405  0.7034        0        1        0        1        1

変化の方向の識別:

direct.cols<-gsub("score", "direction", score.cols)
compute.direction<-function(mydata){
for (i in 1:length(score.cols))
{ 
direct.cols[,i] <- ifelse((period.cols[i] == 1) & (score.cols[i] >= score.cols[i-1]), 1, 
+ ifelse((period.cols[i] == 1) & (score.cols[i] <= score.cols[i-1]), 2,
+ ifelse((period.cols[i] != 1), 0, NA)))
}}
cbind(mydata, `names<-`(lapply(mydata[score.cols], compute.direction), direct.cols))

問題: 最後のステップを実行すると、次のエラー メッセージが表示されます。

    Error in direct.cols[, i] <- ifelse((period.cols[i] == 1) & (score.cols[i] >=  : 
  incorrect number of subscripts on matrix

なんで?そして、私は何を間違っていますか?

どんな助けでも大歓迎です。どうもありがとう。

この質問は、私が以前に尋ねた質問[ https://stackoverflow.com/questions/12443202/how-to-get-the-difference-in-value-between-subsequent-observations-国年]

4

2 に答える 2

1

オブジェクトperiod.colsはベクトルなので 1 次元です。使用する

period.cols[i]

iそれの th 値にアクセスします。

于 2012-09-16T12:41:55.587 に答える
1

以前の質問に対して私が提案したことを再現しようとすると (http://stackoverflow.com/questions/12443202/how-to-get-the-difference-in-value-between-subsequent-observations-country-year)の場合、compute.diffスコアのベクトルのみを入力として受け取る関数にする必要があります。score.Aデータ内の、などの各列に適用さscore.Bれます。したがって、次のようなものを使用する必要があります。

compute.direction <- function(x) {
   x.diff <- c(NA, diff(x))
   ifelse(x.diff > 0.5, 1,
          ifelse(x.diff < -0.5, 2,
                 NA))
}

ただし、前の質問に対する私の回答に加えた編集を見てください。最適なデータ構造で作業していないように思えます。列の複数のブロック ( の場合は 5 つ、 のperiod場合は 5 つdirection) を追加する代わりに、最初に生の (再形成されていないデータ) で作業することをお勧めします。

mydata <- within(mydata, period    <- ave(score, country, FUN = compute.period),
                         direction <- ave(score, country, FUN = compute.direction))

その後、データのみを再形成します。

于 2012-09-16T13:23:20.627 に答える