Passing matrix columns as arguments to a function

Hi there,
I'm trying to use the rows of a matrix as arguments for a function. I've been looking at posts about apply and mapply functions but can't seem to obtain the results I want.

given :

> matr
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    0    1    1    1    1
[2,]    0    0    1    1    0    0    1    1
[3,]    0    1    0    1    0    1    0    1
> func <- function(a,b,c){
+   return(ifelse ((a== b) & (b==c),T,F))
+ }

I want to pass each column elements as the respective arguments a,b,c for the function func, meaning column 1 equates to func(0,0,0) ; column 2 equates to func(0,0, 1), and so on....
and obtain a matrix of one row and as many columns as the source matrix, with the result of the test for each column :

> res_matr
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    T    F    F    F   F    F    F    T

I will actually use a more complicated function and will need to test each "column" (combination of arguments) several times (because the function will have a random nature), in order to identify what "column" always gives the same test result, so for example, a replicate(4, ...) will yield :

> res_matr
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    T    F    F    F   F    F    F    T
[2,]    T    F    F    F   F    F    F    T
[3,]    T    F    F    F   F    F    F    T
[4,]    T    F    F    F   F    F    F    T

My attempts so far have yielded
Error in ifelse((a == b) & (b == c), T, F) : missing argument "b"...
or too many rows with confusing output ; basically, I don't know what to put in place of the ??? here :
res_matr <- apply(matr, MARGIN=2, FUN = func, ???).

Any help would be appreciated. Thanks.

Necessarily limited by the example data and function

# final element changed from 0 to 1 to conform to desired output

dat <- c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1)
m <- matrix(dat, nrow = 3, ncol = 8)
colSums(m) == 0 | colSums(m) == 3
#> [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

As I said, the goal is :

not checking the values in each column.

Usually, when how turns out to be difficult, it's because of losing sight of what.

Every R problem can be thought of with advantage as the interaction of three objects— an existing object, x , a desired object,y , and a function, f, that will return a value of y given x as an argument. In other words, school algebra— f(x) = y. Any of the objects can be composites.

In the code given in the OP, x is a matrix dim 3 8, m and y is a logical vector of length 8. The objective is to populate y according to the equality tests for m[1,][1] == m[2,][2] and m[1,][1] == m[3,][1], which could be generalized to every other row if desired. For this use case, colSums is convenient, but not necessary. An alternative is presented below. Understand the application of the arguments to the subset operator, [m[,] subsets everything, m[1,] returns all columns for row 1, m[,1] returns all rows for column 1, m[2:3,3:4]andm[c(1,3),c(5,7)]` specified rows and columns.

See the FAQ: How to do a minimal reproducible example reprex for beginners to better frame questions.

# final element changed from 0 to 1 to conform to desired output

dat <- c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1)
m <- matrix(dat, nrow = 3, ncol = 8)

a_function <- function(x) colSums(x) == 0 | colSums(x) == 3

a_function(m)
#> [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

another_function <- function(x,y) pi * m[y,] > 0

another_function(m,1:3)
#>       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7] [,8]
#> [1,] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE TRUE
#> [2,] FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE TRUE
#> [3,] FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE TRUE

a_third_function <- function(x,y,z) x[y+1,] == x[y+z,] 

a_third_function(m,1,2)
#> [1]  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE

The what here doesn't matter.
This is a straight forward theoretical question, which utility goes beyond the example given.

You are showing me how to do better what the example function does, but I don't care about that. The example function serves as a theoretical basis for an answer to my theoretical question. What this function actually does, and what you are addressing, is irrelevant. What I want to achieve is this :

I want to pass a column as an ensemble of arguments to any given function.
How can I do that ?

It is a theoretical matter. It's a matter of How. Either tell me that I can, and how, or that I can't. I do not care about some other or better way to achieve the what shown here for the sake of giving a basis to an answer to the how, which, again, is the only thing I care about.

While I thank you for showing me the colSums function, which might be useful to me at some point in the future, you are not answering my question.

@Credenza, why do you have to be so rude?

I do not think I was being particularly rude by merely pointing the fact that I asked a question twice and wasn't given an answer to my actual question, which I tried to explain further for clarification.

Imagine asking someone how to code a t-test in python, and that someone tells you, "here, just use R studio t.test() function instead". You ask him again, clearly, "this is not what I asked, I would like to know how to do that myself using python", and that someone replies, again: "just do this instead".
Is pointing out to that person that he is not answering your question rude ?
I don't think so.

However, my apologies. I do know how people are easily offended by simple facts these days, I probably should have given up on my quest of knowledge in order to avoid causing (unjustified) offense. That's my bad.

You may find Stackoverflow better suited to your style.

dat <- c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1)
mymatrix <- matrix(dat, nrow = 3, ncol = 8)


v_func <- function(m){
  t(as.matrix(m[1,] == m[2,] & m[2,] ==m[3,] ))
}

v_func(mymatrix)

I thank Technocrat for having contributed the reprex for this question.

I may.
Thank you for your time anyway, and good continuation to you here (I feel compelled to underline that I am not being sarcastic).
Cheers.

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.