意外输出 - For Loop

时间:2017-11-24 15:35:53

标签: r for-loop rmysql

我在R中运行嵌套的for循环。我已经从SQL查询创建了一个名为dataframe的数据帧。我的数据被安排为3个数据库的组。以下

CampaignGUID-这有一个列,我通过它引用其他列 数据帧

 for (i in length(nrow(dataframe$CampaignGUID))){
  sql1 <- paste("SELECT T.TenantLookupID, T.TenantShortCode, 
  B.BusinessUnitShortCode, C.CampaignShortCode
            from TenantLookup AS T
            JOIN BusinessUnitLookup AS B ON T.TenantLookupID = 
             B.TenantLookupID
            JOIN CampaignLookup AS C ON C.TenantLookupID = B.TenantLookupID 
            where T.TenantID = '611c5d1c_f0bc_4bd4_b1c1_f6800d50fc34' AND 
           B.BusinessUnitID = 0
            AND C.CampaignID =", i,sep="")
           dataframe2<-dbGetQuery(jobdbconn, sql1)

第二部分

      blasttable <- paste("rptcampaignblast_",(dataframe$CampaignGUID))

     blasttable2<-gsub("-", '_', blasttable)
     blasttable3<-sub(' ', '', blasttable2)
    (blasttable3)
    as.data.frame(blasttable3)

    TenantLookupID1 <- dataframe2$TenantLookupID
     TenantShortCode1 <- dataframe2$TenantShortCode
     BusinessUnitShortCode1 <- dataframe2$BusinessUnitShortCode
     CampaignShortCode1 <- dataframe2$CampaignShortCode


    sql2 <- paste("select count(DISTINCT BINARY R1) from ", blasttable3)

    blastcount<-dbGetQuery(reportconn, sql2)

    sql3 <- paste("select count(DISTINCT BINARY R1) from 
   rptcampaignopentransaction where T1 ='", TenantShortCode1, "' AND B1 ='", 
     BusinessUnitShortCode1, "'  AND  C1 ='", CampaignShortCode1, "'")

    opencount<-dbGetQuery(reportconn, sql3)

    sql4 <- paste("select count(DISTINCT BINARY R1) from 
    rptcampaignlinkclickstransaction where T1 ='", TenantShortCode1, "' AND 
    B1 ='", BusinessUnitShortCode1, "'  AND  C1 ='", CampaignShortCode1, 
    "'")

     linkclickcount<-dbGetQuery(reportconn, sql4)

   sql5 <- paste("select count(DISTINCT BINARY R1) from 
   rpt_mailbounces.rptcampaignmailbounce where T1 ='", TenantShortCode1, "' 
  AND B1 ='", BusinessUnitShortCode1, "'  AND  C1 ='", CampaignShortCode1, 
  "'")

  bounchcount<-dbGetQuery(reportconn, sql5)

  delivered <- (blastcount-bounchcount)






     DF2$CampaignGUID1<-CampaignGUID
    DF2$Bounced<-bounchcount
   DF2$delivered=delivered
    DF2$reach= blastcount-bounchcount
  DF2$blastcount=blastcount
  DF2$click=linkclickcount
  DF2$open=opencount




   DF2<-rbind(DF2$Bounced, DF2$delivered, DF2$reach, 
  DF2$blastcount,DF2$click, DF2$open, DF2$CampaignGUID)

  } #for i

linkclickcount,bounchcount等的最终输出应该是值列表。但输出是单个值。我不确定错误是什么。

0 个答案:

没有答案