R: Apply function to matrix with elements of vector as argument
Mapply
is definitely a possibility. This should work:
mapply(MYFUNC, x = as.data.frame(t(df)), Var = var2)
#V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#5.0795111 2.8693537 1.8285747 1.3640238 0.8300597 0.6280441 0.7706310 0.6720132 0.5719003 0.4259674
The issue I think you were running into is that mapply
takes either vectors or lists. In R matrices aren't lists, but data.frame
s are. All you need to do is transpose your matrix and convert to a data.frame
and then mapply
should work. Each column in a data.frame
is an element in the list which is why we have to transpose it (so that each row
will be mapped to each element in the vector).
how to apply function with two matrices and one vector as input variables
?mapply
may be helpful. Does this work for you?
Below fun
is a function that accepts three arguments.
x1 <- read.table(
text = ' 1.103990 1.345141 1.671156 2.041531 2.435917
1.031078 1.109853 1.229888 1.380356 1.552349
1.018405 1.065593 1.139852 1.236245 1.349988
1.013136 1.046822 1.100638 1.171764 1.257230
1.010249 1.036439 1.078646 1.135048 1.203625
1.008425 1.029847 1.064566 1.111308 1.168612
1.007169 1.025289 1.054776 1.094688 1.143918'
)
x2 <- read.table(
text = ' 6.27530 15.29211 28.49757 46.41790 69.23123
10.96466 23.60472 39.23650 58.71576 82.53972
14.17965 29.67335 47.61181 68.85091 93.98208
16.78984 34.69621 54.71981 77.67461 104.17505
19.04558 39.07866 61.00511 85.59340 113.45587
21.06106 43.01689 66.70069 92.83912 122.03283
22.89981 46.62361 71.94674 99.55885 130.04526'
)
x1 <- t(x1) # Expensive operation in case of large data.frames
x2 <- t(x2) # Expensive operation in case of large data.frames
x3 <- 1:5
fun <- function(x1, x2, x3) {
sqrt(x1**2 + x2**2 + x3**2)
}
mapply(fun, x1, x2, x3)
[1] 6.449665 15.480892 28.703732 46.634636 69.454279 11.058340 23.715282 39.370237 58.868038 82.705593 14.251302 29.759758
[13] 47.719846 68.978084 94.124672 16.850079 34.769568 54.813037 77.786362 104.302549 19.098553 39.143529 61.088353 85.694332
[25] 113.572370 21.108888 43.075671 66.776608 92.931896 122.140809 22.943751 46.677749 72.016984 99.645185 130.146372
Using apply with a different function argument for each element's evaluation
You could use mapply
where x
would be your matrix column, and y
the constant. I didn't bother with converting the matrix into a list the smart way, so I have to use unlist
inside the function.
mat <- matrix(1:5, nrow = 10, ncol = 3, byrow = TRUE)
mat.list <- apply(mat, MARGIN = 2, FUN = list)
mapply(FUN = function(x, y) {
sqrt(mean((unlist(x) - y)^2))
}, x = mat.list, y = list(1, 2, 3))
[1] 2.449490 1.732051 1.414214
Using mapply to apply a function with two arguments to every row of two matrices
If we want to apply on each row, then split the matrix by row and pass as a list
mapply(f, asplit(M1, 1), asplit(M2, 1))
Note that mapply
on a matrix
(or a vector
) will loop over each element i.e. here the unit is a single element whereas in data.frame/data.table/tibble
, the single unit is a column. By splitting by row (asplit
- MARGIN = 1
), we get a list
of vectors and here the unit is a list element
As @Adam mentioned in the comments, it may needs to be t
ransposed (Not clear without testing with f
)
Generating a matrix by applying function to all pairs
Either vectorize F
so that you can use it with outer
or do something like below
sapply(c('A', 'B', 'C', 'D', 'E'), function(a)
sapply(c('a', 'b', 'c', 'd', 'e'), function(b)
F(a,b)))
# A B C D E
#a -32 -31 -30 -29 -28
#b -33 -32 -31 -30 -29
#c -34 -33 -32 -31 -30
#d -35 -34 -33 -32 -31
#e -36 -35 -34 -33 -32
Related Topics
Visualizing Distance Between Nodes According to Weights - with R
Is There an Efficient Way to Parallelize Mapply
Ggplot2: Making Changes to Symbols in The Legend
Using Leaflet-Side-By-Side Plugin in R
Change The Color of a Ggplot Geom a Posteriori (After Having Specified Another Color)
Creating an Equal Distance Spatial Grid in R
Means from a List of Data Frames in R
How to Make UI Respond to Reactive Values in for Loop
Tidyr Separate Column Values into Character and Numeric Using Regex
How to Create a Continuous Legend (Color Bar Style) for Scale_Alpha
Tidyr: Multiple Unnesting with Varying Na Counts
Inserting Logo into Beamer Presentation Using R Markdown
How to Round Percentage to 2 Decimal Places in Ggplot2
How to Append R Data Frame into Existing Excel Without Overwriting
Ifelse Assignment in Data.Table
How to Use Custom Cross Validation Folds with Xgboost
How to Place +/- Plus Minus Operator in Text Annotation of Plot (Ggplot2)