热线电话:13121318867

登录
2018-10-07 阅读量: 3162
R语言中assign函数在循环中出现的问题

代码如下:

for(i in 13:36){

assign(paste0("plot", i),ggplot(data = DOI, aes(x = origin_lat, y = DOI[,i], color = species))+

geom_smooth(method = "lm", formula = y~I(x*x)+x)+

geom_point()+guides(color = FALSE)+

labs(title = "", x = "", y = names(DOI)))

}

然后得到的结果是,总共14张图,变量名为plot13~36,每张图都是i=36的时候的数据做出的图,而纵坐标的label却是i=13~36。

但是如果我单独赋值i的话,运行前面代码中ggplot部分,就是正常的:

ggplot(data = DOI, aes(x = origin_lat, y = DOI[,i], color = species))+

geom_smooth(method = "lm", formula = y~I(x*x)+x)+

geom_point()+guides(color = FALSE)+

labs(title = "", x = "", y = names(DOI))

289.3747
2
关注作者
收藏
评论(1)

发表评论
clipsbj
2018-10-08
请运行体会下面的代码你就会明白: for (i in 1:3){ assign(paste("a", i, sep = ""), i:10) }
0.0000 3 0 回复