Do any generic functions in the base library dispatch on matrices? - r

I'm having some trouble writing some code that dispatches on matrices. To assist me, I'd like to see what generic functions in the base library dispatch on matrices. Is there any way to get R to give me a list of them? Failing that, does anyone know of any members of that list?

There are at least seven functions in base R that have matrix generics:
anyDuplicated
determinant
duplicated
isSymmetric
subset
summary
unique
you can get them with
getS3method("anyDuplicated", class="matrix")
or just
anyDuplicated.matrix
Found using
Filter(function(x) {
!is.null(getS3method(x, class="matrix", optional=TRUE))
},ls(all.names=TRUE, env = baseenv()))

Related

R repeating a function

I have a list with not limited count: parameter<-2,1,3,4,5......
And I would like to repeat a function with the parameter:
MyFunction('2')
MyFunction('1')
MyFunction('3') etc.
Thank you very much for any tips
Like most things in R, there's more than one way of handling this problem. The tidyverse solution is first, followed by base R.
purrr/map
I don't have detail about your desired output, but the map function from the purrr package will work in the situation you describe. Let's use the function plus_one() to demonstrate.
library(tidyverse) # Loads purrr and other useful functions
plus_one <- function(x) {x + 1} # Define our demo function
parameter <- c(1,2,3,4,5,6,7,8,9)
map(parameter, plus_one)
map returns a list, which isn't always desired. There are specialized versions of map for specific kinds of output. Depending on what you want to do, you map_chr, map_int, etc. In this case, we could use map_dbl to get a vector of the returned values.
map_dbl(parameter, plus_one)
Base R
The apply family of functions from base R could also meet your needs. I prefer using purrr but some people like to stick with built-in functions.
lapply(parameter, plus_one)
sapply(parameter, plus_one)
You end up with the same results.
identical({map(parameter, plus_one)}, {lapply(parameter, plus_one)})
# [1] TRUE

What does the t in tapply stand for?

There seems to be general agreement that the l in "lapply" stands for list, the s in "sapply" stands for simplify and the r in "rapply" stands for recursively. But I could not find anything on the t in "tapply". I am now very curious.
Stands for table since tapply is the generic form of the table function. You can see this by comparing the following calls:
x <- sample(letters, 100, rep=T)
table(x)
tapply(x, x, length)
although obviously tapply can do more than counting.
Also, some references that refer to "table-apply":
R and S Plus companion
Modern Applied Biostatistical Methods
I think of it as 'table'-apply since the result comes as a matrix/table/array and its dimensions are established by the INDEX arguments. An R table-classed object is really very similar in contrcution and behavior to an R matrix or array. The application is being performed in a manner similar to that of ave. Groups are first assembled on the basis of the "factorized" INDEX argument list (possibly with multiple dimensions) and a matrix or array is returned with the results of the FUN applied to each cross-classified grouping.
The other somewhat similar function is 'xtabs'. I keep thinking it should have a "FUN" argument, but what I'm probably forgetting at that point is really tapply.
tapply is sort of the odd man out. As far as I know, and as far as the R documentation for the apply functions goes, the 't' does not stand for anything, unlike the other apply functions which indicate the input or output options.

Use the multiple variables in function in r

I have this function
ANN<-function (x,y){
DV<-rep(c(0:1),5)
X1<-c(1:10)
X2<-c(2:11)
ANN<-neuralnet(x~y,hidden=10,algorithm='rprop+')
return(ANN)
}
I need the function run like
formula=X1+X2
ANN(DV,formula)
and get result of the function. So the problem is to say the function USE the object which was created during the run of function. I need to run trough lapply more combinations of x,y, so I need it this way. Any advices how to achieve it? Thanks
I've edited my answer, this still works for me. Does it work for you? Can you be specific about what sort of errors you are getting?
New response:
ANN<-function (y){
X1<-c(1:10)
DV<-rep(c(0:1),5)
X2<-c(2:11)
dat <- data.frame(X1,X2)
ANN<-neuralnet(DV ~y,hidden=10,algorithm='rprop+',data=dat)
return(ANN)
}
formula<-X1+X2
ANN(formula)
If you want so specify the two parts of the formula separately, you should still pass them as formulas.
library(neuralnet)
ANN<-function (x,y){
DV<-rep(c(0:1),5)
X1<-c(1:10)
X2<-c(2:11)
formula<-update(x,y)
ANN<-neuralnet(formula,data=data.frame(DV,X1,X2),
hidden=10,algorithm='rprop+')
return(ANN)
}
ANN(DV~., ~X1+X2)
And assuming you're using neuralnet() from the neuralnet library, it seems the data= is required so you'll need to pass in a data.frame with those columns.
Formulas as special because they are not evaluated unless explicitly requested to do so. This is different than just using a symbol, where as soon as you use it is evaluated to something in the proper frame. This means there's a big difference between DV (a "name") and DV~. (a formula). The latter is safer for passing around to functions and evaluating in a different context. Things get much trickier with symbols/names.

How to rbind matrices based on objects names?

I have several matrices that I would like to rbind in a single summary one. They are objects product of different functions and they have share a pattern in their names.
What I want to do is to tell R to look for all the objects with that common pattern and then rbind them.
Assuming these matrices exist:
commonname.N1<-matrix(nrow=2,ncol=3)
commonname.N2<-matrix(nrow=2,ncol=3)
commonname.M1<-matrix(nrow=2,ncol=3)
I tried something like this to get them:
mats<-grep(x= ls(pos=1), pattern="commonname.", value=TRUE)
mats
[1] "commonname.N1" "commonname.N2" "commonname.M1"
What I can't figure out is how to tell rbind to use that as argument. Basically I would something that gives the same matrix than what rbind(commonname.N1, commonname.N2, commonname.M1) would do in this example.
I have tried things on the line of
mats<-toString(mats)
rbind(mats2)
but that just creates a matrix with the different objects as names.
A similar question was asked here, but:
mats<-as.list(mats)
do.call(what=rbind, args=as.list(mats))
doesn't do the job.
Sorry if there is something basic I'm missing somewhere, but I can't figure it out and I'm relatively new to R.
Use mget:
do.call(rbind,mget(mats))

R - where can vectorize happen?

So clearly one way to vectorize a function is WITHIN the function - either explicitly iterate over inputs or utilize other functions that have been vectorized. Is there a way to mark or tag a function as being/treated as vectorized so that the iteration is managed by the R platform? The analogy would be attributes in c# or annotations in Java. I tell R that this function should be treated as vectorized and it feeds that input one at a time into the function, constructing the vector output? Or am I just thinking about this whole thing incorrectly?
You can use the Vectorize function (http://stat.ethz.ch/R-manual/R-patched/library/base/html/mapply.html), to make the function take vectors.
But here it just uses the mapply function to do the vectorization. As Gavin said, you are just hiding the loop.

Resources