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.