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
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.10.5
[2,] -999.0 -999.0
[3,]
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
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
4 matches
Mail list logo