# [R] Create Arrays

dpender d.pender at civil.gla.ac.uk
Fri Oct 15 11:54:43 CEST 2010

```Barry, Gerrit,

That was what I am after but unfortunately only the starting point.  I am
now trying to amend a function that inserts the R matrices into a dataset in
the correct places:

i.e.

H <- c(0.88, 0.72, 0.89, 0.93, 1.23, 0.86, 0.98, 0.85, 1.23)

T <- c(7.14, 7.14, 7.49, 8.14, 7.14, 7.32, 7.14, 7.14, 7.14)

O <- c(0, 0, 0, 3, 0, 0, 0, 2, 0)

R <- lapply(O[O!=0], function(x){matrix(-1,1,x)})

m <- rbind(H, T)

O2 <- rbind(O,O)

f <- function(x, y, z) {
if(nrow(x) != nrow(y) || nrow(y) != length(z))
stop('unequal numbers of rows among inputs')
out <- matrix(NA,2,length(H)+sum(O))
for(i in 1:2)
out[[i,]] <- append(x[i,], as.numeric(z[[i]]), after = which(y[i,] >
0) - 1)
out
}

f(m, O2, R)

f is the function that requires amendment and was originally written for a
single R value and treated each row of out separately.

I now want to insert R[i] into m before every point that O[i] > 0

Hope this makes sense.

Doug

I don

--
View this message in context: http://r.789695.n4.nabble.com/Create-Arrays-tp2996706p2996776.html
Sent from the R help mailing list archive at Nabble.com.

```