Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中的apply结构化循环中使用计数器_R_Plot_Counter_Apply - Fatal编程技术网

在R中的apply结构化循环中使用计数器

在R中的apply结构化循环中使用计数器,r,plot,counter,apply,R,Plot,Counter,Apply,我试图从R中的一个相当复杂的数组中进行绘图。我想生成一个包含3×3个图形的图像,每个图形上都有红色和蓝色的点 我有一个apply循环的结构,它可以工作,但是我想改变每行的y最大值 我通常会像我一样,用其他语言使用计数器来实现这一点。但是R中的应用程序完全让我困惑 par(mfrow=c(3,3),pty="s") # a 3 by 3 graphic set.seed(1001) x <- 1:54 # wi

我试图从R中的一个相当复杂的数组中进行绘图。我想生成一个包含3×3个图形的图像,每个图形上都有红色和蓝色的点

我有一个apply循环的结构,它可以工作,但是我想改变每行的y最大值

我通常会像我一样,用其他语言使用计数器来实现这一点。但是R中的应用程序完全让我困惑

par(mfrow=c(3,3),pty="s")             # a 3 by 3 graphic
set.seed(1001)

x <- 1:54                             # with 1 to 54 along the x axis

y <- array(rexp(20), dim=c(54,6,3,2)) # and the y axis coming 
                                      # from an array with dimensions as shown.

ymax <- c(1,0.1,0.3)                  # three different y maximum values I want 
                                      # on the graphic, one for each row of graphs

counter <- 1                          # a counter, starting at 1, 
                                      # as I would use in a traditional loop

apply(y[,3:5,,], 2, function(i)       # my first apply, which only considers
                                      # the 3rd, 4th and 5th columns
    {

    yy <- ymax[counter]               # using the counter to select my ylimit maximum

    apply(i, 2, function (ii)         # my second apply, considering the 3rd 
                                      # dimension of y
        {
            plot(x,ii[,1], col="blue", ylim=c(0,yy)) 

                                      # plotting the 4th dimension

                points(x,ii[,2], col="red") 

                                      # adding points in a different 
                                      # colour from the 4th dim. 

    })
})
par(mfrow=c(3,3),pty=“s”)#一个3乘3的图形
种子集(1001)

x我不打算重写您的代码,因为我必须说这很难理解,但这会有所帮助:您可以使用
更新
apply
范围之外的变量,我认为在这种情况下使用循环可能更容易。
此外,您的代码没有更新计数器的行,就像
counter这里的关键是在索引上使用lappy,而不是在数组本身上,因此您可以使用索引来子集y限制和内部循环前面的数组。这也避免了必须使用
如果您只想使用诸如打印之类的侧面效果,则不应使用
apply
。使用
进行循环或重塑数据,并使用ggplot2或lattice。嘿,感谢您提供的有关的提示嘿,谢谢,我以前从未使用过lapply,但我会尝试一下。我以前使用过循环,但是速度太慢了,我换成了应用程序。谢谢你的提示。非常感谢。邪恶,谢谢你。我仍然在思考不同的apply变体,这很好地说明了差异。谢谢again@Kate2808,请参阅前面的评论,忘记除非我引用您,否则不会通知您。
counter <- 0
lapply(1:3, function(x) {
  counter <<- counter + 1
  cat("outer", counter, "\n")
  plot(1:10, main=counter)
})
counter <- 0
lapply(1:3, function(x) {
  counter <<- counter + 1
  cat("outer", counter, "\n")
  lapply(1:3, function(x) {
    counter <<- counter + 1
    cat("inner", counter, "\n") 
    plot(1:10, main=counter)     
  })
})
par(mfrow=c(3,3),pty="s")             # a 3 by 3 graphic
set.seed(1001)
x <- 1:10                             # with 1 to 54 along the x axis
dims <- c(10,6,3,2)
y <- array(rexp(prod(dims)), dim=c(10,6,3,2)) # and the y axis coming 
ymax <- c(1,0.1,0.3)

lapply(1:3, function(counter, arr) {
  apply(
    arr[ ,counter + 2, , ], 2, 
    function(ii) {
      plot(x, ii[,1], col="blue", ylim=c(0,ymax[counter]))
      points(x, ii[,2], col="red") 
    } )
  },
  arr=y
)