assume I have a user defined fun
fun <- function(i,j,k){}
I am looking for a function(), that
function(fun, c(x1, x2), c(y1,y2), c(z1,z2))
performs as
fun(x1,y1,z1)
fun(x1,y2,z1)
fun(x2,y1,z1)
fun(x2,y2,z1)
fun(x1,y1,z2)
fun(x1,y2,z2)
fun(x2,y1,z2)
fun(x2,y2,z2)
I though apply family may do the job. However I did not find the right one.
=====================================================================
Here is a real example I am working on, however still cannot be solved.
data <- read.table(header = TRUE, stringsAsFactors = FALSE,
text = "ID x1 x2 y1 y2
1 a T 100 2
2 b T 210 4
3 b F 112 5
4 a F 121 1
5 b F 412 1")
boxplot <- function(i,j){
print(ggplot(data,
aes_string(x=colnames(data)[i],
y=colnames(data)[j],
col=colnames(data)[i]))+
geom_boxplot())
}
then you can use boxplot(2,4)
to make a plot, so does boxplot(3,4), boxplot(2,5), boxplot(3,5).
Then I tried both do.call and apply methods from answers.
do.call(boxplot, as.list(unname(expand.grid(c(2, 3), c(4,5)))))
or
apply(expand.grid(c(2,3), c(4,5)),1,boxplot)
however they did not work as expected. the do.call method only returns one plot, and the apply one retures 8 plot but all have y axis as ID.
======================================================
the for loop made it work. Any comments?
for (i in 2:3) {
for (j in 4:5) {
boxplot(i,j)
}
}