Hi:
The essential problem is that after you append items, the result is a list
with possibly unequal lengths. Trying to convert that into a data frame by
the 'usual' methods (do.call(rbind, ...) or ldply() in plyr) didn't work (as
anticipated). One approach is to initialize a maximum size matrix with NAs
and then replace the NAs by the contents of each component of the list. The
following function is far from elegant, but the idea is to output a data
frame by 'NA filling' the shorter vectors in the list and doing the
equivalent of do.call(rbind, list).
listNAfill <- function(l) {
# input argument l is a list with numeric component vectors
lengths <- sapply(l, length)
m <- matrix(NA, nrow = length(l), ncol = max(lengths))
for(i in seq_len(length(l))) m[i, ] <- replace(m[i, ], 1:lengths[i],
l[[i]])
as.data.frame(m)
}
# From the previous mail:
u <- f(m, o, l)
> u
[[1]]
[1] 0.88 0.72 0.89 1.20 1.40 0.93 1.23 0.86
[[2]]
[1] 7.14 7.14 1.60 7.49 8.14 7.14 7.32
> listNAfill(u)
V1 V2 V3 V4 V5 V6 V7 V8
1 0.88 0.72 0.89 1.20 1.40 0.93 1.23 0.86
2 7.14 7.14 1.60 7.49 8.14 7.14 7.32 NA
The result of listNAfill is a data frame, so you could cbind or otherwise
attach the date-time info and then use write.table() or some other
appropriate write function.
I'm sure there is some built-in way to NA fill the list components so that
they all have equal length, but I couldn't find it. The rbind.fill()
function from plyr works with data frame inputs, not lists. A quick search
of the archives on 'NA fill' didn't turn up anything I could use, either,
but I didn't look very hard. This seems to work as expected on the simple
example I used.
Better approaches are welcomed...
HTH,
Dennis
On Thu, Oct 14, 2010 at 1:34 AM, dpender <[email protected]> wrote:
>
> Thanks Dennis.
>
>
>
> One more thing if you don't mind. How to I abstract the individual H and T
> arrays from f(m,o,l) so as I can combine them with a date/time array and
> write to a file?
>
>
> Sorry if its a simple question but Im completely new to R.
>
>
>
> Cheers,
>
>
>
> Doug
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Data-Gaps-tp2993317p2994997.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [email protected] 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.
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.