-1

Problem:

I want to compare a 2-dimensional array and a scalar variable, both with numerical values with (<=) lessthanorequalto operation and assign all those values in the array to a vector.

I want to speed up this task in R.

Now, below is the code I am using (which is obviously quite time consuming)

Code I am using now :

2d_examplearray; # My 2-dimensional array actual size 3500 X 4200 my_scalarvariable=5; # some arbitrary value as this is an example

dims_2darray=dim(2d_examplearray); # Get no. of rows & columns information

# First create and then initialize vectors for storing values accordingly as specified in if # condition below

eachelementin_ltvector<-vector();
eachelementin_gtvector<-vector();

eachelementin_ltvector=1;
eachelementin_gtvector=1;

for (eachrow in 1 : dims_2darray[1])
{
for (eachcol in 1 : dims_2darray[2])
{
if(2d_examplearray[eachrow,eachcol]<my_scalarvariable)
{
vector_lessthanvalue[eachelementin_ltvector]=2d_examplearray[eachrow,eachcol];
eachelementin_ltvector=eachelementin_ltvector+1;
}
else # greater than or equal to my scalar variable then
{
vector_greaterthanvalue[eachelementin_gtvector]=2d_examplearray[eachrow,eachcol];
eachelementin_gtvector=eachelementin_gtvector+1;
}
}
}

Thanks for the inputs on my previous post regarding the same question. I am new to R and this Q&A forum.

Thanks again

G.L.P
  • 7,119
  • 5
  • 25
  • 41
  • 3
    Please show a small example and expected output based on that. For guidelines, check [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – akrun Jul 10 '15 at 12:07
  • 1
    Did you try to google `R subsetting`? – nicola Jul 10 '15 at 12:13
  • 1
    `indx <- yourmatrix<= scalarvariable; yourmatrix[indx] <- vector1` But, the lengths of vector1 should be the same as the length of `yourmatrix`. Without a reproducible example, it is not easy to code – akrun Jul 10 '15 at 12:13

3 Answers3

0

You need to provide a reproducible example, but your solution will probably be of the form

m= 3 # 3500
n= 4 # 4200
set.seed(123)
m <- matrix(rnorm(m*n), m, n)
m
#            [,1]       [,2]       [,3]       [,4]
# [1,] -0.5604756 0.07050839  0.4609162 -0.4456620
# [2,] -0.2301775 0.12928774 -1.2650612  1.2240818
# [3,]  1.5587083 1.71506499 -0.6868529  0.3598138

v = 1.5 # the value you want elements less than or equal to

m <= v
#       [,1]  [,2] [,3] [,4]
# [1,]  TRUE  TRUE TRUE TRUE
# [2,]  TRUE  TRUE TRUE TRUE
# [3,] FALSE FALSE TRUE TRUE

But you need to specify how you want the matrix to be mapped from two to one dimensions.

For example, you could do

unlist(m[m <= v])

Which as you can see goes top to bottom, left to right.

#    [1] -0.56047565 -0.23017749  0.07050839  0.12928774  0.46091621 -1.26506123 -0.68685285 -0.44566197  1.22408180  0.35981383
Community
  • 1
  • 1
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
0

From your question I can't say if that's exactly what you are looking for, but I created an example that might be helpful:

mat <- matrix(1:14700000, 3500, 4200)

vector <- c(-1:-1000000)
scalar <- 1000000
mat[mat <= scalar]  <- c(-1:-1000000)

Note that vector must be the same length of the elements being replaced.

Paulo MiraMor
  • 1,582
  • 12
  • 30
0

'which' might be useful:

> A <- matrix( sample(3500*4200)/(3500*4200),3500,4200 )

> b <- 0.3

> v <- A[which(A<=b)]

> system.time( for (n in 1:100) { A[which(A<=b)] } )
       User      System verstrichen 
      30.61        6.05       37.78 

> summary(v)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
7.00e-08 7.50e-02 1.50e-01 1.50e-01 2.25e-01 3.00e-01 

> w <- unlist(A[A<=b])

> system.time( for ( n in 1:100) { unlist(A[A<=b]) } )
       User      System verstrichen 
      32.51        7.22       40.14 

> summary(w)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
7.00e-08 7.50e-02 1.50e-01 1.50e-01 2.25e-01 3.00e-01 

> identical (v,w)
[1] TRUE
mra68
  • 2,960
  • 1
  • 10
  • 17