R在循环中创建对象,具体取决于我 [英] R Create objects in loop named depending on i

查看:57
本文介绍了R在循环中创建对象,具体取决于我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎与 for循环以输出不同的对象存在相同的问题在r 中,但即使考虑此处的解决方案,我也无法理解(抱歉): 我想根据日历年创建对象(可以是矩阵,向量或其他形式).

I have nearly the same problem as for loop to output different objects in r but I don't get it even considering the solution here (Sorry): I want to create objects (can be matrix, vector or else) depending on calender years.

我将实际年份创建为数字:

I create the actual year as number:

now<-Sys.time()
actualyear<-strftime(now, format="%Y")
actualyear<-as.numeric(actualyear)
class(actualyear)

在这种情况下,我想创建7个对象,分别名为test2011,test2012,test2013,.... test2017,以等价的日历年作为内容(test2011 <-2011,test2012<-2012,....)

In this case I want to create 7 objects named test2011, test2012, test2013, .... test2017 with the equivilant calender year as content (test2011 <- 2011, test2012 <- 2012, ....)

for(i in 2011:actualyear) {test[[i]]<-i}

推荐答案

正如注释中指出的那样,您可以使用assign

As pointed out in comments, you can assign a value to a name with assign

for(i in 2011:actualyear) {
  assign(paste0("test", i), i)
}

这篇关于R在循环中创建对象,具体取决于我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆