带有不同参数的get函数的R循环

时间:2016-06-16 12:14:34

标签: r loops assign

我从以下网址

创建数据框

因为我需要为每个站点运行这个脚本的几个站点(例如在附件示例站点1和41中)然后将所有站点运行到一个

$result = mysqli_query($con," SELECT * FROM `BIMTECH_academy_2016_classes` 
WHERE `RealDate` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY) 
ORDER BY RealDate ASC, RealFrom ASC; ");

    $html = ""; // a variable
    $lastDate;
    while($row = mysqli_fetch_array($result))
    {
        if($lastDate != $row['Date'])
        {
              $lastDate = $row['Date'];
              $html.= "<tr>";
                  $html.= "<td colspan='4' id='date'>" . $row['Date'] . "</td>";
              $html.= "</tr>";
        }
      $html.= "<tr>";
          $html.= "<td>" . $row['CourseName'] . "</td>";
          $html.= "<td>" . $row['From'] . "</td>";
          $html.= "<td>" . $row['To'] . "</td>";
          $html.= "<td>" . $row['InstructorName'] . "</td>";
          //$html.= "<td>" . $row['ClassNumber'] . "</td>";
      $html.= "</tr>";
    }

而不是为每个我想创建循环的网站重复它们。

我正在尝试以下脚本

LS_Bellaboo_Token <- xxx
url_signature_orders_report <- "https://ran-reporting.rakutenmarketing.com/en/reports/signature-orders-report/filters?include_summary=Y&tz=GMT&date_type=transaction"
from <- Sys.Date()-39
to <- Sys.Date()-1

site <- 1 
LinkShare_Report_1 <-
  read.csv(text=rawToChar(
    GET(url_signature_orders_report,
        query=list(token=LS_Bellaboo_Token,
                   start_date=from,
                   end_date=to,
                   network=site))
    [["content"]]),skip = 4, header = TRUE, sep =',')
if (length(LinkShare_Report_US$X..of.Items)>0 )
{LinkShare_Report_US["site"] <-"US"}

site <- 41 
LinkShare_Report_41 <-
  read.csv(text=rawToChar(
    GET(url_signature_orders_report,
        query=list(token=LS_Bellaboo_Token,
                   start_date=from,
                   end_date=to,
                   network=site))
    [["content"]]),skip = 4, header = TRUE, sep =',')
if (length(LinkShare_Report_US$X..of.Items)>0 )
{LinkShare_Report_US["site"] <-"US"}

但它对我不起作用,我是R的新人

1 个答案:

答案 0 :(得分:2)

您需要先创建对象LinkShare_Report_

LinkShare_Report_ <- list()

然后你可以运行

site <- c(1,41)
for (i in site){
 LinkShare_Report_[i] <-
  read.csv(text=rawToChar(
     GET(url_signature_orders_report,
         query=list(token=LS_Bellaboo_Token,
               start_date=from,
               end_date=to,
               network=i))
[["content"]]),skip = 4, header = TRUE, sep =',')
}

由于列表为1到41之间的任何内容创建了空元素,因此使用assign更为简约,为每个站点创建一个新对象。

site <- c(1,41)
for (i in site){
 assign(paste0('site',i),
  read.csv(text=rawToChar(
     GET(url_signature_orders_report,
         query=list(token=LS_Bellaboo_Token,
               start_date=from,
               end_date=to,
               network=i))
[["content"]]),skip = 4, header = TRUE, sep =',')
)
}