Apply function works incorrectly

#1

Hello community,

I was applying the apply function to a matrix, which has values of K both above and below A.

When I run the following command: apply(K>A, 2, all), I get both a number of True and False.

However, when I run apply(K<A, 2, all), I get only "False" values.

Cannot figure it out, pretty desperate right now.

What can be the problem?

0 Likes

#2

This works as expected, it's hard to know what your problem is without seen your data, or at least a sample of it.

# Sample data using dput(K)
K <- structure(c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 5, 4.4, 4.9, 3.5, 
                 3, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 1.4, 1.4, 1.3, 1.5, 
                 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 
                 0.2, 0.2, 0.1), .Dim = c(10L, 4L), .Dimnames = list(NULL, NULL))
A <- 2

K
#>       [,1] [,2] [,3] [,4]
#>  [1,]  5.1  3.5  1.4  0.2
#>  [2,]  4.9  3.0  1.4  0.2
#>  [3,]  4.7  3.2  1.3  0.2
#>  [4,]  4.6  3.1  1.5  0.2
#>  [5,]  5.0  3.6  1.4  0.2
#>  [6,]  5.4  3.9  1.7  0.4
#>  [7,]  4.6  3.4  1.4  0.3
#>  [8,]  5.0  3.4  1.5  0.2
#>  [9,]  4.4  2.9  1.4  0.2
#> [10,]  4.9  3.1  1.5  0.1

K > A
#>       [,1] [,2]  [,3]  [,4]
#>  [1,] TRUE TRUE FALSE FALSE
#>  [2,] TRUE TRUE FALSE FALSE
#>  [3,] TRUE TRUE FALSE FALSE
#>  [4,] TRUE TRUE FALSE FALSE
#>  [5,] TRUE TRUE FALSE FALSE
#>  [6,] TRUE TRUE FALSE FALSE
#>  [7,] TRUE TRUE FALSE FALSE
#>  [8,] TRUE TRUE FALSE FALSE
#>  [9,] TRUE TRUE FALSE FALSE
#> [10,] TRUE TRUE FALSE FALSE
apply(K > A, 2, all)
#> [1]  TRUE  TRUE FALSE FALSE

K < A
#>        [,1]  [,2] [,3] [,4]
#>  [1,] FALSE FALSE TRUE TRUE
#>  [2,] FALSE FALSE TRUE TRUE
#>  [3,] FALSE FALSE TRUE TRUE
#>  [4,] FALSE FALSE TRUE TRUE
#>  [5,] FALSE FALSE TRUE TRUE
#>  [6,] FALSE FALSE TRUE TRUE
#>  [7,] FALSE FALSE TRUE TRUE
#>  [8,] FALSE FALSE TRUE TRUE
#>  [9,] FALSE FALSE TRUE TRUE
#> [10,] FALSE FALSE TRUE TRUE
apply(K < A, 2, all)
#> [1] FALSE FALSE  TRUE  TRUE

Created on 2019-02-08 by the reprex package (v0.2.1)

Could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

0 Likes

closed #6

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes