从for循环创建变量

时间:2015-01-08 10:19:17

标签: r loops dataframe

我想在数据帧中创建大量的顺序变量(比下面的虚拟数据更多)。我尝试过使用for (i in a:b)(见下文),但我无法做到这一点。

 # Create test dataframe #

    test<-as.data.frame(1:15)
    test$X16<-10
    test$P_15<-20
    test$P_16<-25
    colnames(test)[1]<-"X15"

    # Proposed do loop #

    for (i in 15:16){
      test$rate_[[i]]<-1000*test$X[[i]]/test$P_[[i]]
    }

编辑 - Karsten W提出的解决方案完美无缺,谢谢。

for (i in 15:16){
  test[[paste("rate_", i, sep="")]]<-1000*test[[paste("X", i, sep="")]]/test[[paste("P_", i, sep="")]]
}

0 个答案:

没有答案
相关问题