使用lapply的地块列表

编程入门 行业动态 更新时间:2024-10-27 11:19:23
本文介绍了使用lapply的地块列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我最近一直在使用lapply和sapply作为我的首选功能。到目前为止这么好,但为什么下面的代码不能阻止我。

df <-as.data.frame(matrix(rnorm(50),ncol = 5))名称(df)<-c(x1,x2,x3,x4,x5) df1 <-seq_len(10) ll < lapply(seq(1,5),function(i)qplot(df1,df [,i]))

`[.data.frame`(df,,i)中的错误:未定义的列选择

好吧,显然我在可重复的代码中犯了一个不幸的错误。它现在可以工作,但 ll 列表中的所有图都是同一个图。当我运行这个: $ p $ do.call(grid.arrange,ll)

我收到以下图片:

所有地块都是一样的!这也是当我通过我的数据运行时获得的输出。

解决方案

无论如何。您需要执行以下操作:

ll <-lapply( seq(1,5),函数(i)qplot(data = data.frame(y = df [,i]),df1,y))

这会迫使每个图表的y值更新。

在这个其他的讨论中 SO Post 。

I have been using lapply and sapply as my go-to functions recently. So far so good, but why the following code does not work baffles me.

df<-as.data.frame(matrix(rnorm(50),ncol=5)) names(df)<-c("x1","x2","x3","x4","x5") df1<-seq_len(10) ll<-lapply(seq(1,5), function(i) qplot(df1,df[,i]))

I get the error:

Error in `[.data.frame`(df, , i) : undefined columns selected

Ok, apparently I made quite an unfortunate mistake in my reproducible code. It works now, but all the plots in the ll list are the same plot. When I run this:

do.call(grid.arrange,ll)

I get the following image:

All the plots are the same! This is also the output I get when I run this through my data.

解决方案

There are problems with lazy evaluation, or something like it anyway. You need to do the following:

ll<-lapply( seq(1,5), function(i) qplot(data=data.frame(y=df[, i]), df1, y) )

This will force the y values to be updated for each plot.

More discussion in this other SO Post.

更多推荐

使用lapply的地块列表

本文发布于:2023-07-19 17:02:12,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地块   列表   lapply

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!