R中的循环内有多个if语句

时间:2019-07-04 13:54:38

标签: r loops if-statement

我有一个R脚本,可以从Outlook中的每日电子邮件中检索CSV文件,然后根据电子邮件主题中的日期是否大于设置的日期将其移动到特定的文件夹。 该代码将主题行拆分以提取日期-由于最近的更改,该日期的位置可以在字符串的两个位置之一。

我建立了一个if语句,无论哪种情况都可以成功地在字符串中找到日期,但是我不能使用第二条if语句来查看第一条if语句的输出是否大于采样日期。

下面是我要执行的代码(我包含了可以复制的数据):

df <- structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 3L, 4L), t = c(12, 
14, 2, 0.5, 12, 3, 2), group = c(1L, 1L, 6L, 7L, 1L, 1L, 4L)),
class = "data.frame", row.names = c(NA, 
-7L))

尝试执行此代码时,出现以下错误,但是我不确定如何解释它:

# Test data
testLoop <- c("[EXTERNAL] Test Promo Sessions was executed at 28062019 100005",
              "[EXTERNAL] Test Promo Sessions was executed at 29062019 100023",
              "Test Promo Sessions was executed at 30062019 100007",
              "Test Promo Sessions was executed at 01072019 100043",
              "Test Promo Sessions was executed at 02072019 100049",
              "Test Promo Sessions was executed at 03072019 100001")

# Example date
todaysDateFormatted2 <- '30062019'

# Loop
for(i in testLoop){
  if(if(nchar(i) == 51){
    strptime(sapply(strsplit(i, "\\s+"), "[", 7),"%d%m%Y")
  } else {
    strptime(sapply(strsplit(i, "\\s+"), "[", 8),"%d%m%Y")
  } > strptime(todaysDateFormatted2,"%d%m%Y")){
    print("greater than - move file")
  } else {
    print("not greater than - do nothing")
  }
}

2 个答案:

答案 0 :(得分:1)

您的代码中存在几个缺陷。复制的if很奇怪,如果您没有将strptime分配给t以下的内容,那么您将else无处可去。另外,您可能想将t条件分配给t。现在,您可以将todaysDateFormatted2for (i in testLoop) { if (nchar(i) == 51) { t <- strptime(sapply(strsplit(i, "\\s+"), "[", 7),"%d%m%Y") } else { t <- strptime(sapply(strsplit(i, "\\s+"), "[", 8),"%d%m%Y") } if (t > strptime(todaysDateFormatted2,"%d%m%Y")) { print("greater than - move file") } else { print("not greater than - do nothing") } } # [1] "not greater than - do nothing" # [1] "not greater than - do nothing" # [1] "not greater than - do nothing" # [1] "greater than - move file" # [1] "greater than - move file" # [1] "greater than - move file" 进行比较,并打印每次迭代的结果。

{{1}}

答案 1 :(得分:1)

OP中的代码失败,因为R不能将内部if()语句始终解析为长度为1的向量,这会导致外部if()失败,如OP中所述。

如果代码的目的是根据文件名中的日期决定是否移动文件,则更简单的代码版本可以实现所需的功能。在这里,我们通过使用lapply()并将原始内部if()子句的输出保存到对象来降低嵌套级别。然后,我们将保存的对象与代表今天日期的对象进行比较,并向R日志中写入一条消息。

# Test data
testLoop <- c("[EXTERNAL] Test Promo Sessions was executed at 28062019 100005",
              "[EXTERNAL] Test Promo Sessions was executed at 29062019 100023",
              "Test Promo Sessions was executed at 30062019 100007",
              "Test Promo Sessions was executed at 01072019 100043",
              "Test Promo Sessions was executed at 02072019 100049",
              "Test Promo Sessions was executed at 03072019 100001")

# Example date
todaysDateFormatted2 <- '30062019'


datesProcessed <- lapply(testLoop,function(x){
     if(nchar(x) == 51) y <- strptime(sapply(strsplit(x, "\\s+"), "[", 7),"%d%m%Y")
     else y <- strptime(sapply(strsplit(x, "\\s+"), "[", 8),"%d%m%Y")
     if(y > strptime(todaysDateFormatted2,"%d%m%Y")) message("greater than - move file")
     else message("not greater than - do nothing")
     y
})

...以及输出:

> datesProcessed <- lapply(testLoop,function(x){
+      if(nchar(x) == 51) y <- strptime(sapply(strsplit(x, "\\s+"), "[", 7),"%d%m%Y")
+      else y <- strptime(sapply(strsplit(x, "\\s+"), "[", 8),"%d%m%Y")
+      if(y > strptime(todaysDateFormatted2,"%d%m%Y")) message("greater than - move file")
+      else message("not greater than - do nothing")
+      y
+ })
not greater than - do nothing
not greater than - do nothing
not greater than - do nothing
greater than - move file
greater than - move file
greater than - move file
>