Re: [R] Data Gaps
Hi r-help-boun...@r-project.org napsal dne 14.10.2010 10:34:12: 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? Try to look at ?merge function as it could do what you really want. Regards Petr Sorry if it’s a simple question but I’m 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. __ R-help@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. __ R-help@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.
Re: [R] Data Gaps
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 it’s a simple question but I’m 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. __ R-help@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.
Re: [R] Data Gaps
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 d.pen...@civil.gla.ac.uk 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. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
[R] Data Gaps
R community, I am trying to write a code that fills in data gaps in a time series. I have no R or statistics background at all but the use of R is proving to be a large portion of my PhD research. So far my code identifies where and the number of new entries required but I do not know how to add additional rows or columns into an array. Any advice on how this can be done? Here is an example: H [0.88 0.72 0.89 0.93 1.23 0.86] T [7.14 7.14 7.49 8.14 7.14 7.32] O [0 0 0 2 0 0] This says that in order to complete the data set 2 entries are required prior to H[4] and T[4] i.e. where O = 2. Thanks, Doug -- View this message in context: http://r.789695.n4.nabble.com/Data-Gaps-tp2993317p2993317.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Data Gaps
Hi: Perhaps ?append for simple insertions... HTH, Dennis On Wed, Oct 13, 2010 at 1:24 AM, dpender d.pen...@civil.gla.ac.uk wrote: R community, I am trying to write a code that fills in data gaps in a time series. I have no R or statistics background at all but the use of R is proving to be a large portion of my PhD research. So far my code identifies where and the number of new entries required but I do not know how to add additional rows or columns into an array. Any advice on how this can be done? Here is an example: H [0.88 0.72 0.89 0.93 1.23 0.86] T [7.14 7.14 7.49 8.14 7.14 7.32] O [0 0 0 2 0 0] This says that in order to complete the data set 2 entries are required prior to H[4] and T[4] i.e. where O = 2. Thanks, Doug -- View this message in context: http://r.789695.n4.nabble.com/Data-Gaps-tp2993317p2993317.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Data Gaps
Dennis, Thanks for that. The problem now is that I am trying to use it in a for loop. Based on the example before, 2 entries are required after H[3] as specified by O. The problem is that when inserting values the length of the array changes so I don't know how to tell the loop that. This is what I have: for (i in 1:length(H)) { if (o[i]0) {append(H, -1, after = i-1)} } Any ideas how to create a loop that allows the inclusion of more than 1 value? Cheers, Doug djmuseR wrote: Hi: Perhaps ?append for simple insertions... HTH, Dennis On Wed, Oct 13, 2010 at 1:24 AM, dpender d.pen...@civil.gla.ac.uk wrote: R community, I am trying to write a code that fills in data gaps in a time series. I have no R or statistics background at all but the use of R is proving to be a large portion of my PhD research. So far my code identifies where and the number of new entries required but I do not know how to add additional rows or columns into an array. Any advice on how this can be done? Here is an example: H [0.88 0.72 0.89 0.93 1.23 0.86] T [7.14 7.14 7.49 8.14 7.14 7.32] O [0 0 0 2 0 0] This says that in order to complete the data set 2 entries are required prior to H[4] and T[4] i.e. where O = 2. Thanks, Doug -- View this message in context: http://r.789695.n4.nabble.com/Data-Gaps-tp2993317p2993317.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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. -- View this message in context: http://r.789695.n4.nabble.com/Data-Gaps-tp2993317p2993582.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Data Gaps
Hi: On Wed, Oct 13, 2010 at 5:31 AM, dpender d.pen...@civil.gla.ac.uk wrote: Dennis, Thanks for that. The problem now is that I am trying to use it in a for loop. Based on the example before, 2 entries are required after H[3] as specified by O. The problem is that when inserting values the length of the array changes so I don't know how to tell the loop that. This is what I have: If you know what the replacement values are in advance, you can create a list object where each component is a vector of values to insert and then access the i-th component of the list in the loop. If the replacement values are not known in advance, then it's not so obvious to me what you should do. The function f() below takes three inputs: the first is a matrix whose rows contain the original vectors, the second is a list of values to be inserted (not necessarily equal in length from one row to the next), and the third is a matrix whose rows indicate where the insertions should take place in the vector (- 1). which(x 0) returns the index (or indices) where the value of the vector x is positive. The output of the function is a list because the output vectors are not necessarily the same length after insertion. Here's a small example with two row matrices for the inputs and matrices, as well as a two-component list for the inserts: H - c(0.88, 0.72, 0.89, 0.93, 1.23, 0.86) T - c(7.14, 7.14, 7.49, 8.14, 7.14, 7.32) O - c(0, 0, 0, 2, 0, 0) R - c(1.2, 1.4) m - rbind(H, T) o - rbind(O, c(0, 0, 1, 0, 0, 0)) l - list(R1 = R, R2 = 1.6) f - function(mval, midx, mrep) { if(nrow(mval) != nrow(midx) || nrow(midx) != length(l)) stop('unequal numbers of rows among inputs') n - nrow(mval) out - vector('list', n) for(i in seq_len(n)) out[[i]] - append(mval[i, ], as.numeric(l[[i]]), after = which(midx[i, ] 0) - 1) out } f(m, o, l) [[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 HTH, Dennis for (i in 1:length(H)) { if (o[i]0) {append(H, -1, after = i-1)} } Any ideas how to create a loop that allows the inclusion of more than 1 value? Cheers, Doug djmuseR wrote: Hi: Perhaps ?append for simple insertions... HTH, Dennis On Wed, Oct 13, 2010 at 1:24 AM, dpender d.pen...@civil.gla.ac.uk wrote: R community, I am trying to write a code that fills in data gaps in a time series. I have no R or statistics background at all but the use of R is proving to be a large portion of my PhD research. So far my code identifies where and the number of new entries required but I do not know how to add additional rows or columns into an array. Any advice on how this can be done? Here is an example: H [0.88 0.72 0.89 0.93 1.23 0.86] T [7.14 7.14 7.49 8.14 7.14 7.32] O [0 0 0 2 0 0] This says that in order to complete the data set 2 entries are required prior to H[4] and T[4] i.e. where O = 2. Thanks, Doug -- View this message in context: http://r.789695.n4.nabble.com/Data-Gaps-tp2993317p2993317.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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. -- View this message in context: http://r.789695.n4.nabble.com/Data-Gaps-tp2993317p2993582.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.