简体   繁体   中英

Store Loop results in dataframe R

I want to store looped results of AIC fit into a dataframe, when I try to print within the block, the dataframe is created successfully, but when I called the dataframe outside of the loop, it seems to be empty, I'm wondering what could be the problem. Here is the code.

A<-data.frame(AIC_ori=numeric(0),AIC_len=numeric(0),AIC_2dl=numeric(0))
for (i in 1:17){
  fit.ori <- glc(key_resp_3.keys ~ ori_transform, data=d[d$block== i,], category=d[d$block== i,]$corrAns, zlimit=7)
  fit.len <- glc(key_resp_3.keys ~ length, data=d[d$block== i,], category=d[d$block== i,]$corrAns, zlimit=7)
  fit.2dl <- glc(key_resp_3.keys ~ ori_transform + length, data=d[d$block== i,], category=d[d$block== i,]$corrAns, zlimit=7)
  #This code compares the three by calculating an AIC. Lower is better
  A[i,1]=AIC(fit.ori)
  A[i,2]= AIC(fit.len)
  A[i,3]=AIC(fit.2dl)
  print(A)
}

  

With no data the following is untested but I believe it does what the for loop is supposed to do.

A <- lapply(1:17, function(i){
  k <- d$block == i
  fit.ori <- glc(key_resp_3.keys ~ ori_transform, data = d[k, ], category = d$corrAns[k], zlimit = 7)
  fit.len <- glc(key_resp_3.keys ~ length, data = d[k, ], category = d$corrAns[k], zlimit = 7)
  fit.2dl <- glc(key_resp_3.keys ~ ori_transform + length, data = d[k, ], category = d$corrAns[k], zlimit = 7)
  data.frame(AIC_ori = AIC(fit.ori), AIC_len = AIC(fit.len), AIC_2dl = AIC(fit.2dl))
})

A <- do.call(rbind, A)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM