j**p 发帖数: 53 | 1 would appreciate your help on this on:
I am doing a bunch of lm (~ 100) and wanted to save the results (
coefficients, p value, etc.) nicely to a list
so I have
fit_res = list()
for(n in 1:100)
{
this_fit = lm (....) //I have 5 covariates, so this_fit has 6 coefficients
fit_res$coeffs[n] = this_fit$coefficients
}
however, fit_res$coeffs only contains the first coefficient of each fit, not
the complete set of 6.
thanks! | R*****0 发帖数: 146 | 2 没有上下文,不过感觉fit_res$coeffs被默认设成了数组,下标[n]只能用于数组(
矩阵,向量)的运算和读写。试试这个:
fit_res <- list(coeffs = vector("list",100))
for (n in 1:100){
this_fit <- lm(...)
fit_res$coeffs[[n]] <- this_fit$coef
}
fit_res$coeffs[[50]][1:6]
以后直接调用双括号下标[[n]] | w****r 发帖数: 28 | 3 fit_res$coeffs[n] 改成
fit_res$coeffs[[n]]
看看 | b********r 发帖数: 764 | 4 为什么不specify好呢?
fit_res$coeffs<-matrix(nrow=100,ncol=6)
fit_res$coeffs[n,]<-as.vector(this_fit$coefficients)
【在 j**p 的大作中提到】 : would appreciate your help on this on: : I am doing a bunch of lm (~ 100) and wanted to save the results ( : coefficients, p value, etc.) nicely to a list : so I have : fit_res = list() : for(n in 1:100) : { : this_fit = lm (....) //I have 5 covariates, so this_fit has 6 coefficients : fit_res$coeffs[n] = this_fit$coefficients : }
| c***z 发帖数: 6348 | 5 try append new rows
instead of filling blanks
【在 j**p 的大作中提到】 : would appreciate your help on this on: : I am doing a bunch of lm (~ 100) and wanted to save the results ( : coefficients, p value, etc.) nicely to a list : so I have : fit_res = list() : for(n in 1:100) : { : this_fit = lm (....) //I have 5 covariates, so this_fit has 6 coefficients : fit_res$coeffs[n] = this_fit$coefficients : }
| C***i 发帖数: 486 | 6 fit_res=rbind(fit_res, this_fit$coefficients) |
|