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.