Hi I have a little vector function that takes a vector A of strictly positive integers and outputs a matrix M each of whose columns is the vector, modified in a complicated combinatorical way. Now I want to generalize the function so that A can include zeroes. Given A, I want to strip out the zeroes, pass it to my function, and pad M with rows at positions corresponding to the zeroes of A. Commented, minimal, self-contained, reproducible toy example follows. f <- function(a){cbind(a,a+1,rev(a))} #real function a ghastly nightmare A <- 1:5 f(A) a [1,] 1 2 5 [2,] 2 3 4 [3,] 3 4 3 [4,] 4 5 2 [5,] 5 6 1 # f() works as desired. # Now introduce A2, that includes zeroes. In my application, f(A2) would fail because of the zeroes. A2 <- c(1,0,0,2,4,0,3) I can strip the zeroes out and call f(): f(A2[A2>0]) a [1,] 1 2 3 [2,] 2 3 4 [3,] 4 5 2 [4,] 3 4 1 which is fine. How to put the zeroes back in in the appropriate rows and get the following: > cbind(c(1,0,0,2,4,0,3),c(2,0,0,3,5,0,4),c(3,0,0,4,2,0,1)) [,1] [,2] [,3] [1,] 1 2 3 [2,] 0 0 0 [3,] 0 0 0 [4,] 2 3 4 [5,] 4 5 2 [6,] 0 0 0 [7,] 3 4 1 > anyone? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
This is not as elegant, but should work: a3 <- f(A2) a3[ which( apply(a3,1,prod) == 0 ), ] <- rep(0,ncol(a3)) a3 Essentially use the product to pick out the rows with at least one 0 and replace these rows with 0s. HTH. -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Robin Hankin Sent: Thursday, July 27, 2006 9:54 AM To: RHelp Subject: [R] inserting rows into a matrix Hi I have a little vector function that takes a vector A of strictly positive integers and outputs a matrix M each of whose columns is the vector, modified in a complicated combinatorical way. Now I want to generalize the function so that A can include zeroes. Given A, I want to strip out the zeroes, pass it to my function, and pad M with rows at positions corresponding to the zeroes of A. Commented, minimal, self-contained, reproducible toy example follows. f <- function(a){cbind(a,a+1,rev(a))} #real function a ghastly nightmare A <- 1:5 f(A) a [1,] 1 2 5 [2,] 2 3 4 [3,] 3 4 3 [4,] 4 5 2 [5,] 5 6 1 # f() works as desired. # Now introduce A2, that includes zeroes. In my application, f(A2) would fail because of the zeroes. A2 <- c(1,0,0,2,4,0,3) I can strip the zeroes out and call f(): f(A2[A2>0]) a [1,] 1 2 3 [2,] 2 3 4 [3,] 4 5 2 [4,] 3 4 1 which is fine. How to put the zeroes back in in the appropriate rows and get the following: > cbind(c(1,0,0,2,4,0,3),c(2,0,0,3,5,0,4),c(3,0,0,4,2,0,1)) [,1] [,2] [,3] [1,] 1 2 3 [2,] 0 0 0 [3,] 0 0 0 [4,] 2 3 4 [5,] 4 5 2 [6,] 0 0 0 [7,] 3 4 1 > anyone? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743 ______________________________________________ R-help at stat.math.ethz.ch 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.
Hello Robin, Here's a solution. library(micEcon) nozeros <-f(A2[A2>0]) zeroslines <- which(A2==0) #identify rows to insert zeros withzeros <-nozeros #initialize matrix with zeros for (i in zeroslines){ withzeros <- insertRow(withzeros,i,0) } withzeros Neurooo>From: Robin Hankin <r.hankin at noc.soton.ac.uk> >To: RHelp <r-help at stat.math.ethz.ch> >Subject: [R] inserting rows into a matrix >Date: Thu, 27 Jul 2006 14:54:02 +0100 > >Hi > > >I have a little vector function that takes a vector A of strictly >positive integers >and outputs a matrix M each of whose columns is the vector, modified in >a complicated combinatorical way. > >Now I want to generalize the function so that A can include zeroes. >Given A, >I want to strip out the zeroes, pass it to my function, and pad M > with rows at positions corresponding to the zeroes of A. > >Commented, minimal, self-contained, reproducible toy example follows. > > >f <- function(a){cbind(a,a+1,rev(a))} #real function a ghastly >nightmare > > A <- 1:5 > f(A) > a >[1,] 1 2 5 >[2,] 2 3 4 >[3,] 3 4 3 >[4,] 4 5 2 >[5,] 5 6 1 > > ># f() works as desired. > ># Now introduce A2, that includes zeroes. In my application, f(A2) >would fail >because of the zeroes. > >A2 <- c(1,0,0,2,4,0,3) > >I can strip the zeroes out and call f(): > f(A2[A2>0]) > a >[1,] 1 2 3 >[2,] 2 3 4 >[3,] 4 5 2 >[4,] 3 4 1 > >which is fine. How to put the zeroes back in in the appropriate rows >and get the following: > > > cbind(c(1,0,0,2,4,0,3),c(2,0,0,3,5,0,4),c(3,0,0,4,2,0,1)) > [,1] [,2] [,3] >[1,] 1 2 3 >[2,] 0 0 0 >[3,] 0 0 0 >[4,] 2 3 4 >[5,] 4 5 2 >[6,] 0 0 0 >[7,] 3 4 1 > > > > > >anyone? > > > >-- >Robin Hankin >Uncertainty Analyst >National Oceanography Centre, Southampton >European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > >______________________________________________ >R-help at stat.math.ethz.ch 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.
Try this where f and A2 are as in your post: out <-f(A2[A2>0]) replace(matrix(0, length(A2), ncol(out)), A2 > 0, out) On 7/27/06, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:> Hi > > > I have a little vector function that takes a vector A of strictly > positive integers > and outputs a matrix M each of whose columns is the vector, modified in > a complicated combinatorical way. > > Now I want to generalize the function so that A can include zeroes. > Given A, > I want to strip out the zeroes, pass it to my function, and pad M > with rows at positions corresponding to the zeroes of A. > > Commented, minimal, self-contained, reproducible toy example follows. > > > f <- function(a){cbind(a,a+1,rev(a))} #real function a ghastly > nightmare > > A <- 1:5 > f(A) > a > [1,] 1 2 5 > [2,] 2 3 4 > [3,] 3 4 3 > [4,] 4 5 2 > [5,] 5 6 1 > > > # f() works as desired. > > # Now introduce A2, that includes zeroes. In my application, f(A2) > would fail > because of the zeroes. > > A2 <- c(1,0,0,2,4,0,3) > > I can strip the zeroes out and call f(): > f(A2[A2>0]) > a > [1,] 1 2 3 > [2,] 2 3 4 > [3,] 4 5 2 > [4,] 3 4 1 > > which is fine. How to put the zeroes back in in the appropriate rows > and get the following: > > > cbind(c(1,0,0,2,4,0,3),c(2,0,0,3,5,0,4),c(3,0,0,4,2,0,1)) > [,1] [,2] [,3] > [1,] 1 2 3 > [2,] 0 0 0 > [3,] 0 0 0 > [4,] 2 3 4 > [5,] 4 5 2 > [6,] 0 0 0 > [7,] 3 4 1 > > > > > > anyone? > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
replace 0 with NA and then back again:> ffunction(a){cbind(a,a+1,rev(a) )}> f(1:5)a [1,] 1 2 5 [2,] 2 3 4 [3,] 3 4 3 [4,] 4 5 2 [5,] 5 6 1> x <- c(1,0,3,4,0,5) > f(x)a [1,] 1 2 5 [2,] 0 1 0 [3,] 3 4 4 [4,] 4 5 3 [5,] 0 1 0 [6,] 5 6 1> x[x==0] <- NA > f(x)a [1,] 1 2 5 [2,] NA NA NA [3,] 3 4 4 [4,] 4 5 3 [5,] NA NA NA [6,] 5 6 1> y <- f(x) > y[is.na(y)] <- 0 > ya [1,] 1 2 5 [2,] 0 0 0 [3,] 3 4 4 [4,] 4 5 3 [5,] 0 0 0 [6,] 5 6 1>On 7/27/06, Robin Hankin <r.hankin@noc.soton.ac.uk> wrote:> > Hi > > > I have a little vector function that takes a vector A of strictly > positive integers > and outputs a matrix M each of whose columns is the vector, modified in > a complicated combinatorical way. > > Now I want to generalize the function so that A can include zeroes. > Given A, > I want to strip out the zeroes, pass it to my function, and pad M > with rows at positions corresponding to the zeroes of A. > > Commented, minimal, self-contained, reproducible toy example follows. > > > f <- function(a){cbind(a,a+1,rev(a))} #real function a ghastly > nightmare > > A <- 1:5 > f(A) > a > [1,] 1 2 5 > [2,] 2 3 4 > [3,] 3 4 3 > [4,] 4 5 2 > [5,] 5 6 1 > > > # f() works as desired. > > # Now introduce A2, that includes zeroes. In my application, f(A2) > would fail > because of the zeroes. > > A2 <- c(1,0,0,2,4,0,3) > > I can strip the zeroes out and call f(): > f(A2[A2>0]) > a > [1,] 1 2 3 > [2,] 2 3 4 > [3,] 4 5 2 > [4,] 3 4 1 > > which is fine. How to put the zeroes back in in the appropriate rows > and get the following: > > > cbind(c(1,0,0,2,4,0,3),c(2,0,0,3,5,0,4),c(3,0,0,4,2,0,1)) > [,1] [,2] [,3] > [1,] 1 2 3 > [2,] 0 0 0 > [3,] 0 0 0 > [4,] 2 3 4 > [5,] 4 5 2 > [6,] 0 0 0 > [7,] 3 4 1 > > > > > > anyone? > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help@stat.math.ethz.ch 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 you are trying to solve? [[alternative HTML version deleted]]