R 如何根据指示变化方向的两个观测值之间的差异创建新变量?

R 如何根据指示变化方向的两个观测值之间的差异创建新变量?,r,variables,loops,dataframe,R,Variables,Loops,Dataframe,想象一下,你有五个国家在十年内的表现分数。你知道,一些国家的业绩在特定年份发生了很大变化。现在,你想知道他们的变化是积极的还是消极的。困扰我的是这最后一步 样本数据: 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

想象一下,你有五个国家在十年内的表现分数。你知道,一些国家的业绩在特定年份发生了很大变化。现在,你想知道他们的变化是积极的还是消极的。困扰我的是这最后一步

样本数据:

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))

mydata对象
period.cols
是一个向量,因此是一维的。使用

period.cols[i]

访问it的
i
th值。

如果您尝试复制我为您前面的问题所建议的内容(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))
}

然后只对数据进行重塑。

啊,谢谢!我删除了逗号,并相应地编辑了上述条目。现在,我得到一条新的错误消息:
direct.cols[,I]=:矩阵上的下标数不正确
。有什么想法吗?Juhu——我只是把它应用到我自己的数据集上,它是有效的:)这是一个非常好的主意,不改变数据的形状,使它变得不那么复杂。非常感谢你!
compute.direction <- function(x) {
   x.diff <- c(NA, diff(x))
   ifelse(x.diff > 0.5, 1,
          ifelse(x.diff < -0.5, 2,
                 NA))
}
mydata <- within(mydata, period    <- ave(score, country, FUN = compute.period),
                         direction <- ave(score, country, FUN = compute.direction))