How do I get rid of list elements where the value is NULL before applying rbind?
Here is a function I use to return the non-NULL elements of a list:
delete.NULLs <- function (x.list)
{
x.list[unlist(lapply(x.list, length) != 0)]
}
On Thu, Jul 22, 2010 at 2:22 PM, Ted Byers <r.ted.byers at gmail.com> wrote:
Here is the function that makes the data.frames in the list:
funweek <- function(df)
?if (length(df$elapsed_time) > 5) {
? ?res = fitdist(df$elapsed_time,"exp")
? ?year = df$sale_year[1]
? ?sample = df$sale_week[1]
? ?mid = df$m_id[1]
? ?estimate = res$estimate
? ?sd = res$sd
? ?samplesize = res$n
? ?loglik = res$loglik
? ?aic = res$aic
? ?bic = res$bic
? ?chisq = res$chisq
? ?chisqpvalue = res$chisqpvalue
? ?chisqdf = res$chisqdf
? ?if (!is.null(estimate) && !is.null(sd) && !is.null(loglik) &&
!is.null(aic) && !is.null(bic) &&
? ? ? ?!is.null(chisq) && !is.null(chisqpvalue) && !is.null(chisqdf)) {
? ? ?rv =
data.frame(mid,year,sample,samplesize,estimate,sd,loglik,aic,bic,chisq,chisqpvalue,chisqdf)
? ? ?rv
? ?}
?}
I use the following, with different data, successfully:
z <-
lapply(split(moreinfo,list(moreinfo$m_id,moreinfo$sale_year,moreinfo$sale_week),drop
= TRUE), funweek)
qqq <- z[, c('mid', 'year', 'sample', 'samplesize', 'estimate', 'sd',
'loglik', 'aic','bic', 'chisq', 'chisqpvalue', 'chisqdf')]
ndf2 <- do.call(rbind, qqq)
However, I am now getting the following error:
qqq <- z[, c('mid', 'year', 'sample', 'samplesize', 'estimate', 'sd',
'loglik', 'aic','bic', 'chisq', 'chisqpvalue', 'chisqdf')]
Error in z[, c("mid", "year", "sample", "samplesize", "estimate", "sd", ?:
? incorrect number of dimensions
My suspicion is that it is due to the fact that sometimes one or more of the elements in my conditional block is null, so nothing is returned and that this puts a null element into z. ?Here is a selection of a couple elements so you can see what is in 'z'. $`353.2010.0`
? ? ?mid year sample samplesize ? estimate ? ? ? ? sd ? loglik ? ? ?aic rate 353 2010 ? ? ?0 ? ? ? ? 17 0.06463837 0.01567335 -63.5621 129.1242 ? ? ? ? ? bic ? ?chisq chisqpvalue chisqdf rate 129.9574 14.90239 0.001901994 ? ? ? 3 $`355.2010.0` NULL $`376.2010.0` ? ? ?mid year sample samplesize ? estimate ? ? ? ? sd ? ?loglik ? ? ?aic rate 376 2010 ? ? ?0 ? ? ? ? ?6 0.07228863 0.02950606 -21.76253 45.52506 ? ? ? ? ? bic ? ?chisq ?chisqpvalue chisqdf rate 45.31682 16.46848 4.946565e-05 ? ? ? 1
You see the value for rowname = `355.2010.0` is NULL., and it is my guess that this leads to the error I show above. ?But I can't confirm that yet, because I don't yet know how to get rid of rows that have a row name but only NULL as the value. I haven't seen this dealt with in the references I have read so far. I think I may be able to deal with it by creating dummy values for the fields the data frame requires, and then use SQL to remove them, but I'd rather not have to resort to that if I can avoid it. I can't believe there isn't something in the base package for R that would easily handle this, but not knowing the name of the function to look at, I haven't found it yet. Any information would be appreciated. Thanks Ted ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?