遍历R中引号的列表项

时间:2019-05-23 06:17:29

标签: r iteration quotes

我正在尝试从列表中迭代名称中带有引号的项目。到目前为止,我已经管理了以下内容:

lm_uni1=matrix(nrow=100,ncol=2)
      for(i in 5:100){
      #t1.port.returns<-cbind(as.data.frame(stress.list$`item:i`[[1]]),
      #                       as.data.frame(stress.list$`item:i`[[2]]),
      #                       as.data.frame(stress.list$`item:i`[[3]]),
      #                       as.data.frame(stress.list$`item:i`[[4]])) 
      #The above iteration does not work because the i is between ` ` and thus
#it does not change over the iteration process.
#The next one tries to adjust for the issue:          
t1.port.returns<-cbind(
                cat(paste("stress.list$`item:",i,"`[[1]]",sep="")),
                cat(paste("stress.list$`item:",i,"`[[2]]",sep="")),
                cat(paste("stress.list$`item:",i,"`[[3]]",sep="")),
                cat(paste("stress.list$`item:",i,"`[[4]]",sep=""))
 )


              names(t1.port.returns)<-c("PortMU","PortS","PortA","PortN")
              AnaDataset<-as.data.frame(cbind(t1.port.returns,FF))
              names(AnaDataset)[6]<-"F5"
              seq1<-summary(lm(PortMU~RF+F1+F2+F3+F4+F5,data=AnaDataset))
              lm_uni1[i,1]<-seq1$coef[2,1]
              lm_uni1[i,2]<-seq1$coef[2,4]
              }

但是t1.port.returns返回以下内容:

stress.list$`item:5`[[1]]stress.list$`item:5`[[2]]stress.list$`item:5`[[3]]stress.list$`item:5`[[4]]NULL

不是数据矩阵。 cat()不能帮助访问与列表项名称关联的数据。有没有办法解决这个问题?

列表中一项的输出为:

> head(stress.list$`item:50`[[1]])
[1] 0.0005571963 0.0007163872 0.0006470110 0.0007732345 0.0012316404 0.0010932486
> length(stress.list$`item:50`[[1]])
[1] 189

因此,t1.port.returns的输出基本上应该像这样:

>head(cbind(stress.list$`item:50`[[1]],stress.list$`item:50`[[2]],stress.list$`item:50`[[3]]))
             [,1]         [,2]         [,3]
[1,] 0.0005571963 0.0005476554 0.0007157594
[2,] 0.0007163872 0.0007142794 0.0007953541
[3,] 0.0006470110 0.0007206310 0.0006402598
[4,] 0.0007732345 0.0008514236 0.0009244147
[5,] 0.0012316404 0.0012356093 0.0013711927
[6,] 0.0010932486 0.0009898300 0.0012163905

但是在实现cat()对其进行迭代时,我们没有上面的输出。

0 个答案:

没有答案