2

I want to compare the first row of an array with the rest of them. So I am running the following code but the results are not as expected. For example flag[2, 1] is FALSE instead of TRUE (2 > 1). Any idea what I am doing wrong ?

yy <- array(data = c(1:16), dim = c(4,4))
yy
#     [,1] [,2] [,3] [,4]
#[1,]    1    5    9   13
#[2,]    2    6   10   14
#[3,]    3    7   11   15
#[4,]    4    8   12   16

flag <- (yy >= yy[1, ])
flag
#      [,1]  [,2]  [,3] [,4]
#[1,]  TRUE  TRUE  TRUE TRUE
#[2,] FALSE  TRUE  TRUE TRUE
#[3,] FALSE FALSE  TRUE TRUE
#[4,] FALSE FALSE FALSE TRUE

Thank you all.

0

1 Answer 1

3

R stores arrays in columns, not in rows. You want:

t(t(yy) >= yy[1,])

Another approach is:

yy >= matrix(yy[1,], nrow(yy), ncol(yy), byrow = TRUE)
Sign up to request clarification or add additional context in comments.

1 Comment

You are right. I didn't know that. Thank you a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.