意外输出 - For 循环

Unexpected output-For Loop

我是 运行 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 等的最终输出应该是一个值列表。然而,输出是单个值。我不确定错误是什么。

确实很难理解您的代码,但是,我看到了一些问题:

  1. 你的for循环控制语句正在做for(i in length(nrow(df))){... 您应该尝试评估 length(nrow(df)) 其中 returns 只是一个长度为 1 的向量。因此 i 的计算结果为 1。要解决此问题,请执行以下操作:

    for(i in 1:nrow(df)){...

  2. 您没有将查询结果分配给列表。如果您只是重新 运行 循环的每次迭代,它将覆盖您的数据。您需要执行以下操作:

    query_results <- list()

    query_results[[i]] <- dbGetQuery(jobdbconn, sql1)

  3. 您的 SQL 过滤器在以下位置采用 i 的原始形式(或只是 1 的值):

    paste(...AND C.CampaignID =", i,sep="")

    您实际上需要通过以下方式将 SQL 过滤器设置为 campaignGUID:

    paste(...AND C.CampaignID =", dataframe$CampaignGUID[i], ,sep="")

总的来说,我建议您手动评估代码的每个部分一次,并在编写 for 循环之前查看输出。祝你好运