Closed djhocking closed 9 years ago
This seems to work
for(i in 2:length(names(B))) {
df[ , names(B[i])][is.na(df[ , names(B[i])])] <- colMeans(B[i])
}
I have been trying to move away from using for()
loops in R since they are so slow, but in this case the loop would never be longer than ~10 (the number of covariates in the model) so maybe it's not a problem.
did you try ifelse()?. it's vectorised.
On Tue, Nov 18, 2014 at 9:45 PM, Daniel J. Hocking <notifications@github.com
wrote:
This seems to work
for(i in 2:length(names(B))) { df[ , names(B[i])][is.na(df[ , names(B[i])])] <- colMeans(B[i]) }
I have been trying to move away from using for() loops in R since they are so slow, but in this case the loop would never be longer than ~10 (the number of covariates in the model) so maybe it's not a problem.
— Reply to this email directly or view it on GitHub https://github.com/Conte-Ecology/conteStreamTemperature/issues/21#issuecomment-63583982 .
Silvio O. Conte Anadromous Fish Research Center, U.S. Geological Survey P.O. Box 796 -- One Migratory Way Turners Falls, MA 01376 (413) 863-3803 Cell: (413) 522-9417 FAX (413) 863-9810
ben_letcher@usgs.gov bletcher@eco.umass.edu http://www.lsc.usgs.gov/?q=cafb-research
When I run the code
It seems to work correctly when
var.name = "site"
(orhuc
) but not whenvar.name = "year"
. It works correctly when the year is indata
andB
but not when that particular year is missing fromB
(i.e. trying to predict to unobserved years).I believe that it's a problem with this line
Rather than the mean of column 2 in
B
replacing all theNA
in the correspondingly named column indf
it repeats the values inB
down each selected column indf
.Those first 10 rows in
intercept.year.B.year
should all be-0.0046
and all shown indOY.B.year
should be0.040347204
, etc.This code is left over from when I was replacing all the values with 0 (which worked) but then I realized that the random effects weren't all centered on 0 so I have to replace them by the mean.
Is there a good way to do this when I don't know what the names of the columns will be in advance and the length with change depending on the covariates in the model? The names and lengths will always be available in
cov.list
andcoef.list
.Sorry if this is confusing. I was realizing what the underlying problem was as I was writing it.