-2

我再次发现自己在 R 中执行重复性任务,并相信可能有更聪明或至少更短的方法来处理以下任务。我JK.M*Y****在 data.frame 中为每个月和每个年创建一个新变量。它是根据 data.frame 中的现有变量计算的,包括一个ifelse语句,每个月和一年的语句。

首先,R 中是否有针对此类重复性任务的默认方法?其次,有没有更聪明的方法来做我下面具体做的事情?

# Example Data with 2 months, 2 years and 3 variables
DF<- structure(list(ID = 1:4, ABC.M1Y2001 = c(10, 12.3, 45, 89), ABC.M2Y2001 = c(11.1, 
          34, 67.7, -15.6), ABC.M1Y2002 = c(-11.1, 9, 34, 56.5), ABC.M2Y2002 = c(12L,
          13L, 11L, 21L), DEF.M1Y2001 = c(14L, 14L, 14L, 16L), DEF.M2Y2001 = c(15L,
          15L, 15L, 12L), DEF.M1Y2002 = c(5, 12, 23.5, 34), DEF.M2Y2002 = c(6L,
          34L, 61L, 56L), GHI.M1Y2001 = c(18.3, 2.8, 9.5, 28.2), 
          GHI.M2Y2001 = c(-0.90, 21.1, 57, -36.7), GHI.M2Y2002 = c(0.52, 
          -12.2, -32.9, 21.2), GHI.M1Y2002 = c(-11, -1.7, -5.7, -17)), 
          .Names = c("ID", "ABC.M1Y2001", "ABC.M2Y2001","ABC.M1Y2002", 
          "ABC.M2Y2002", "DEF.M1Y2001", "DEF.M2Y2001", "DEF.M1Y2002", 
          "DEF.M2Y2002", "GHI.M1Y2001","GHI.M2Y2001","GHI.M1Y2002","GHI.M2Y2002"), 
          class = "data.frame", row.names = c(NA, -4L))

# 2001 create new variable "JK" for each month per year
DF$JK.M1Y2001 <- ifelse(((4 * DF$ABC.M1Y2001)+(2*DF$DEF.M1Y2001))/5 < 0,
                         DF$GHI.M1Y2001 / (.6* exp(((2*DF$DEF.M1Y2001)/(DF$DEF.M1Y2001+7)))),
                         DF$GHI.M1Y2001 / (.6* exp(((7*DF$DEF.M1Y2001)/(DF$DEF.M1Y2001+3)))))

DF$JK.M2Y2001 <- ifelse(((4 * DF$ABC.M2Y2001)+(2*DF$DEF.M2Y2001))/5 < 0,
                         DF$GHI.M2Y2001 / (.6* exp(((2*DF$DEF.M2Y2001)/(DF$DEF.M2Y2001+7)))),
                         DF$GHI.M2Y2001 / (.6* exp(((7*DF$DEF.M2Y2001)/(DF$DEF.M2Y2001+3)))))
# and so on for 2001
# ...
# 2002 create new variable "JK" for each month per year
DF$JK.M1Y2002 <- ifelse(((4 * DF$ABC.M1Y2002)+(2*DF$DEF.M1Y2002))/5 < 0,
                        DF$GHI.M1Y2002 / (.6* exp(((2*DF$DEF.M1Y2002)/(DF$DEF.M1Y2002+7)))),
                        DF$GHI.M1Y2002 / (.6* exp(((7*DF$DEF.M1Y2002)/(DF$DEF.M1Y2002+3)))))

# ...
4

1 回答 1

1

我会在两个循环中做到这一点:

for(month in c('M1', 'M2')){
  for(year in c('Y2001', 'Y2002')){
    new.var.name <- paste('JK.M', month, year)
    first.var.name <- paste('ABC.M', month, year)
    second.var.name <- paste('ABC.M', month, year)
    third.var.name <- paste('ABC.M', month, year)
    DF[[new.var.name]] <- ifelse(((4 * DF[[first.var.name]])+(2*DF[[second.var.name]]))/5 < 0,
                    DF[[third.var.name]] / (.6* exp(((2*DF$DEF.M1Y2002)/(DF$DEF.M1Y2002+7)))),
                    DF[[third.var.name]] / (.6* exp(((7*DF$DEF.M1Y2002)/(DF$DEF.M1Y2002+3)))))
  }
}

关键是paste用来构造变量名,把数据框当作一个列表来添加新变量。

这可以改进,但我试图把它拼出来,这样你就可以看到这个想法。

于 2013-11-08T20:03:49.600 回答