简体   繁体   English

循环/应用功能可一一运行两个或多个列表

[英]loop/apply function to run two or more lists one by one

assume I have a user defined fun 假设我有一个用户定义的乐趣

fun <- function(i,j,k){}

I am looking for a function(), that 我正在寻找一个function()

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). 那么您可以使用boxplot(2,4)进行绘图,boxplot(3,4),boxplot(2,5),boxplot(3,5)也会如此。

Then I tried both do.call and apply methods from answers. 然后,我尝试了do.call和应用答案中的方法。

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. do.call方法仅返回一个图,而套用一个retures 8个图,但都以y轴作为ID。

====================================================== ================================================== ====

the for loop made it work. for循环使其起作用。 Any comments? 任何意见?

for (i in 2:3) {
  for (j in 4:5) {
  boxplot(i,j)
  }
}

Try this: 尝试这个:

sum3 <- function(x, y, z) x+y+z  # test function
mapply(sum3, 1:3, 11:13, 21:23)
## [1] 33 36 39

Here sum3(1, 11, 21) is 33, sum3(2, 12, 22) is 36 and sum3(3, 13, 23) is 39. There are more examples on the ?mapply help page. 这里sum3(1, 11, 21)是33, sum3(2, 12, 22)是36, sum3(3, 13, 23)?mapply帮助页面上还有更多示例。

This also works and returns a list: 这也可以工作并返回一个列表:

Map(sum3, 1:3, 11:13, 21:23)

xIt would be nice if you provided a reproducible example so we can verify possible solutions, but I think you can get what you want with exapand.grid and do.call . 如果您提供了一个可复制的示例,那么它会很好,以便我们可以验证可能的解决方案,但是我认为您可以通过exapand.griddo.call How about 怎么样

do.call(fun, as.list(unname(expand.grid(c(x1, x2), c(y1,y2), c(z1,z2)))))

Do if you had 如果你有

fun <- function(i,j,k){i+j+k}
x1<-1; x2<-2;
y1<-10; y2<-20;
z1<-100; z2<-200;

You would get 你会得到

[1] 111 112 121 122 211 212 221 222
# x1+y1+z1, x2+y1+z1, x1+y2+z1, x2+y3+z1, ...

If I understood this correctly,this should be pretty simple 如果我理解正确,这应该很简单

apply(expand.grid(c(x1, x2), c(y1,y2), c(z1,z2)),1,fun)

edit : adding example 编辑:添加示例

x <-c(13,14)
y <- c(21,22)
z<- c(35,36)
apply(expand.grid(x,y,z),1,mean)
[1] 23.00000 23.33333 23.33333 23.66667 23.33333 23.66667 23.66667 24.00000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM