Paula Fergnani Salvia
2010-Oct-05 15:54 UTC
[R] Question about assigning values in a matrix, conditional on column first row; how to do the loop.
Hello, I?m new at programming and I will greatly appreciate if you can help me with this. I have a very large matrix (hundreds of rows and columns), with the first raw filled with different numbers (between 0 and 1). The rest of the matrix is filled with values 0, 1, 2. What I need is to replace the values in the matrix (except the first row will has to remain intact). In each column I have to replace value 2 for the value of the first row. Also, I want to replace the values 0 and 1 by -999. I was able to write the loop for a vector but I do not know how to expand the loop to the matrix. Please see these examples For a vector I did this and it works> V = c(0.1,1,0,2) > V[1] 0.1 1.0 0.0 2.0> for (i in 2:length(V)){if(V[i]==2)V[i]=V[1]+ else (V[i]=-999)}> V[1] 0.1 -999.0 -999.0 0.1 For a matrix I could not perform the task. Here it is an example of a small matrix and the expected result, but the original has more rows and columns.> matr <- matrix(c(0.1,1,0,2,0.5,1,2,2), nrow=4, ncol=2) > matr[,1] [,2] [1,] 0.1 0.5 [2,] 1.0 1.0 [3,] 0.0 2.0 [4,] 2.0 2.0> matr.expectedresult= matrix(c(0.1,-999,-999,0.1,0.5,-999,0.5,0.5), nrow=4, ncol=2) > matr.expectedresult[,1] [,2] [1,] 0.1 0.5 [2,] -999.0 -999.0 [3,] -999.0 0.5 [4,] 0.1 0.5 Thank you very much Paula
jim holtman
2010-Oct-05 16:14 UTC
[R] Question about assigning values in a matrix, conditional on column first row; how to do the loop.
try this:> matr[,1] [,2] [1,] 0.1 0.5 [2,] 1.0 1.0 [3,] 0.0 2.0 [4,] 2.0 2.0> result <- apply(matr[-1,], 1, function(.row){+ ifelse(.row == 2, matr[1,], -999) + })> # data back together > rbind(matr[1,], t(result))[,1] [,2] [1,] 0.1 0.5 [2,] -999.0 -999.0 [3,] -999.0 0.5 [4,] 0.1 0.5>On Tue, Oct 5, 2010 at 11:54 AM, Paula Fergnani Salvia <paulafergnani at yahoo.com.ar> wrote:> Hello, I?m new at programming and I will greatly appreciate if you can help me with this. > > > I have a very large matrix (hundreds of rows and columns), with the first raw filled with different numbers (between 0 and 1). The rest of the matrix is filled with values 0, 1, 2. What I need is to replace the values in the matrix (except the first row will has to remain intact). In each column I have to replace value 2 for the value of the first row. Also, I want to replace the values 0 and 1 by -999. I was able to write the loop for a vector but I do not know how to expand the loop to the matrix. > > Please see these examples > > For a vector I did this and it works > >> V = c(0.1,1,0,2) >> V > [1] 0.1 1.0 0.0 2.0 > >> for (i in 2:length(V)){if(V[i]==2)V[i]=V[1] > + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else (V[i]=-999)} > >> V > [1] ? ?0.1 -999.0 -999.0 ? ?0.1 > > For a matrix I could not perform the task. Here it is an example of a small matrix and the expected result, but the original has more rows and columns. > >> matr <- matrix(c(0.1,1,0,2,0.5,1,2,2), nrow=4, ncol=2) >> matr > ? ? [,1] [,2] > [1,] ?0.1 ?0.5 > [2,] ?1.0 ?1.0 > [3,] ?0.0 ?2.0 > [4,] ?2.0 ?2.0 > >> matr.expectedresult= matrix(c(0.1,-999,-999,0.1,0.5,-999,0.5,0.5), nrow=4, ncol=2) >> matr.expectedresult > ? ? ? [,1] ? [,2] > [1,] ? ?0.1 ? ?0.5 > [2,] -999.0 -999.0 > [3,] -999.0 ? ?0.5 > [4,] ? ?0.1 ? ?0.5 > > Thank you very much > > Paula > > > > > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Thomas Stewart
2010-Oct-05 16:31 UTC
[R] Question about assigning values in a matrix, conditional on column first row; how to do the loop.
Paula-
Look at this example. R in this case is your data matrix.
-tgs
R<-matrix(sample(0:2,100,replace=T),nrow=10)
R[1,]<-round(runif(10),3)
f<-function(x){
show(x)
y<-x[-1]
y[y!=2]<--999
y[y==2]<-x[1]
c(x[1],y)
}
apply(R,2,f)
On Tue, Oct 5, 2010 at 11:54 AM, Paula Fergnani Salvia <
paulafergnani@yahoo.com.ar> wrote:
> Hello, I’m new at programming and I will greatly appreciate if you can help
> me with this.
>
>
> I have a very large matrix (hundreds of rows and columns), with the first
> raw filled with different numbers (between 0 and 1). The rest of the matrix
> is filled with values 0, 1, 2. What I need is to replace the values in the
> matrix (except the first row will has to remain intact). In each column I
> have to replace value 2 for the value of the first row. Also, I want to
> replace the values 0 and 1 by -999. I was able to write the loop for a
> vector but I do not know how to expand the loop to the matrix.
>
> Please see these examples
>
> For a vector I did this and it works
>
> > V = c(0.1,1,0,2)
> > V
> [1] 0.1 1.0 0.0 2.0
>
> > for (i in 2:length(V)){if(V[i]==2)V[i]=V[1]
> + else (V[i]=-999)}
>
> > V
> [1] 0.1 -999.0 -999.0 0.1
>
> For a matrix I could not perform the task. Here it is an example of a small
> matrix and the expected result, but the original has more rows and columns.
>
> > matr <- matrix(c(0.1,1,0,2,0.5,1,2,2), nrow=4, ncol=2)
> > matr
> [,1] [,2]
> [1,] 0.1 0.5
> [2,] 1.0 1.0
> [3,] 0.0 2.0
> [4,] 2.0 2.0
>
> > matr.expectedresult= matrix(c(0.1,-999,-999,0.1,0.5,-999,0.5,0.5),
> nrow=4, ncol=2)
> > matr.expectedresult
> [,1] [,2]
> [1,] 0.1 0.5
> [2,] -999.0 -999.0
> [3,] -999.0 0.5
> [4,] 0.1 0.5
>
> Thank you very much
>
> Paula
>
>
>
>
>
> ______________________________________________
> 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]]
jthetzel
2010-Oct-06 03:59 UTC
[R] Question about assigning values in a matrix, conditional on column first row; how to do the loop.
Paula,
Just for fun, another way:
## Create 10 by 10 example matrix
set.seed(100)
a <- matrix(sample(c(0,1,2),100, replace=T), nrow=10, ncol=10)
a[1,] <- sample(c(0.25,0.5,0.75),ncol(a), replace=T)
## Use apply to substitiute matrix values by row
b <- apply(a, 1, function(x)
{
row <- x
row[row==0 | row==1] <- -999
row[which(row==2)] <- a[1,which(x==2)]
return(row)
})
## Transpose result matrix
b <- t(b)
Cheers,
Jeremy
Jeremy Hetzel
Boston University
--
View this message in context:
http://r.789695.n4.nabble.com/Question-about-assigning-values-in-a-matrix-conditional-on-column-first-row-how-to-do-the-loop-tp2956355p2964227.html
Sent from the R help mailing list archive at Nabble.com.