Array in R?

9 views
Skip to first unread message

pell...@gmail.com

unread,
Feb 28, 2019, 6:19:11 AM2/28/19
to Forest-R
Hi All,

I have this array :

arr <- array(data = c(0.1, 0.52, 0.3, 0.4, 0.3, 0.2, 0.7, 0.8), dim = c(2, 2, 2))
I want to test if there is a value > 0.5 so return it if not return NA.

I tried this but it is not what I want :

apply(arr, c(1,2), function(x) {ifelse(x >0.5, x,NA)})
my desire output 1 matrix 2*2:

res1= #resturn the value
[,1] [,2]
[1,] NA 0.7
[2,] 0.52 0.8
and another matrix 2*2 to show the index res2= return the index of that value

[,1] [,2]
[1,] NA 1
[2,] 1 2
1 means 0.7 comes from the first matrix arr[,,1]

2 means 0.8 comes from the second matrix arr[,,2] etc

Any hints on this.
thanks
Thom

Philip Radtke

unread,
Feb 28, 2019, 1:36:28 PM2/28/19
to for...@googlegroups.com
Hi Thom,

Would this work for you?

arr[arr > 0.5] <- NA

cheers,
-Phil

--
You received this message because you are subscribed to the Google Groups "Forest-R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to forestr+u...@googlegroups.com.
To post to this group, send email to for...@googlegroups.com.
Visit this group at https://groups.google.com/group/forestr.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages