我们经常会需要生成这样一类的变量,比如a1,a2,a3......
这时候我们需要用到这两个函数:get()和assign()
get()用法
get()函数只是在环境中搜索该变量名的变量,如果该变量不存在则返回异常
a2 = 1 get(paste0("a","2")) ls()[grep("a2", ls())]
assign()用法
assign()功能就是对变量进行赋值;assign(x, value, ...) # x为变量名,value为变量值
rm(list = ls()) assign(paste("a","1",sep=""),2) ls()
get与assign联用
for(i in 1:3){ assign(paste("p", i, sep=""), i) tmp <- get(paste("p", i, sep="")) print(tmp) }
很实用