R:用apply函数替换for循环

时间:2015-12-03 14:37:48

标签: r for-loop apply linear-regression

我设法对数据框的每个主题应用线性回归,并使用 for-loop 将值粘贴到新的数据框中。但是,我认为使用apply函数应该有一种更易读的方法来实现我的结果,但我的所有尝试都失败了。我就是这样做的:

numberOfFiles <- length(resultsHick$subject)
intslop       <- data.frame(matrix(0,numberOfFiles,4))
intslop       <- rename(intslop,
                        subject   = X1,
                        intercept = X2,
                        slope     = X3,
                        Rsquare   = X4)

cond        <- c(0:3)
allSubjects <- resultsHick$subject

for (i in allSubjects)
    {intslop[i,1] <- i

     yvalues <- t(subset(resultsHick,
                  subject == i, 
                  select = c(H0meanRT, H1meanRT, H2meanRT, H258meanRT)))

     fit        <- lm(yvalues ~ cond)
     intercept  <- fit$coefficients[1]
     slope      <- fit$coefficients[2]
     rsquared   <- summary(fit)$r.squared
     intslop[i,2] <- intercept
     intslop[i,3] <- slope
     intslop[i,4] <- rsquared
     }

结果应与

相同
> head(intslop)
  subject intercept    slope   Rsquare
1       1  221.3555 54.98290 0.9871209
2       2  259.4947 66.33344 0.9781499
3       3  227.8693 47.28699 0.9537868
4       4  257.7355 80.71935 0.9729132
5       5  197.4659 49.57882 0.9730409
6       6  339.1649 61.63161 0.8213179
...

是否有人知道使用apply函数编写此代码的更易读的方法?

1 个答案:

答案 0 :(得分:0)

我用来替换聚合data.frames的循环的一个常见模式是:

do.call(
  rbind,
  lapply(1:numberOfDataFrames,
         FUN = function(i) {
           print(paste("Processing index:", i)) # helpful to see how slow/fast
           temp_df <- do_some_work[i]
           temp_df$intercept <- 1, etc.
           return(temp_df) # key is to return a data.frame for each index.
         }
  )
)