在R中嵌套的逻辑问题?

时间:2014-05-05 01:40:45

标签: r if-statement nested

我从名为 dateCount.df 的数据框中获得以下数据(见下文)。

您可以看到记录号19是星期日,而前一记录是星期五。我正在尝试编写一个嵌套的if else语句,该语句将采用星期五,星期六和星期日nrec字段值并对它们取平均值并用该平均值替换星期日的nrec。它不适用于记录19.任何人都可以在我的嵌套if中找到逻辑中的错误吗?

suns = which(format(dateCount.df$Date, "%w") == 0)
for (i in suns) {
    # this first "if"  should check if the prior date Date[i - 1]  in dateCount is equal to the actual day before  Date[i]-1  and same for Friday
    if (i <= 2 && dateCount.df$Date[i - 1] == (dateCount.df$Date[i] - 1)  && dateCount.df$Date[i - 2] == (dateCount.df$Date[i] - 2)) {
        dateCount.df$nrec[i] <- (dateCount.df$nrec[i] + dateCount.df$nrec[i - 1] + dateCount.df$nrec[i - 2]) / 3
    }
    else {
        # this "if" should check if the prior date Date[i - 1]  in dateCount is equal to the actual day before  Date[i]-1  but not checking about Friday
        #because to get into this block of the embedded if one of the 3 conditions failed and I am now dealing with missing Friday data
        if (i>=2 &&  dateCount.df$Date[i-1] == (dateCount.df$Date[i] - 1)) {
            # this should be the case where Saturday is available but not Friday, so add and divide by 2
            dateCount.df$nrec[i] <- (dateCount.df$nrec[i] + dateCount.df$nrec[i - 1]) / 2
        }
        else {
            # this "if" should check if the prior date Date[i-2]  in dateCount is equal to the actual day before  Date[i]-2  but not checking about Saturday
            #because to get into this block of the embedded if one of the 3 conditions failed  and also the case where Saturday is available but not Friday
            # and I am now dealing with missing Saturday data but Friday is available.
            if (i <= 2 && (dateCount.df$Date[i - 2] == dateCount.df$Date[i] - 2)) {
                # this should be the case where Friday is available but not Saturday, so add and divide by 2
                dateCount.df$nrec[i] <- (dateCount.df$nrec[i] + dateCount.df$nrec[i - 2]) / 2
            }
            else {
                if (i >= 2) {
                    # this should be the case where neither Friday or Saturday is available so do nothing
                    dateCount.df$nrec[i]<-dateCount.df$nrec[i]
                }
                else { 
                    dateCount.df$nrec[i]<-dateCount.df$nrec[i]
                }
            }
        }
    }
}

数据:

           Date  nrec  DayOfWeek
--------------------------------
 1    7/17/2011   220        Sun
 2    7/18/2011   267        Mon
 3   10/29/2009    30        Thu
 4   10/30/2009   212        Fri
 5   10/31/2009   238        Sat
 6    11/1/2009   424        Sun
 7    11/2/2009   423        Mon
 8    11/3/2009   268        Tue
 9    11/4/2009   445        Wed
10    11/5/2009   331        Thu
11    11/6/2009   241        Fri
12    11/7/2009   236        Sat
13    11/8/2009   332        Sun
14    11/9/2009   421        Mon
15   11/10/2009   399        Tue
16   11/11/2009   323        Wed
17   11/12/2009   358        Thu
18   11/13/2009   238        Fri
19   11/15/2009   301        Sun
20   11/16/2009   439        Mon
21   11/17/2009   374        Tue
22   11/23/2009   145        Mon
23   11/24/2009   472        Tue
24   11/25/2009   331        Wed
25   11/26/2009   327        Thu
26   11/27/2009   261        Fri
27   11/28/2009   296        Sat
28   11/29/2009   461        Sun
29   11/30/2009   514        Mon
30    12/1/2009   656        Tue
31    12/2/2009   505        Wed
32    12/3/2009   535        Thu
33    12/4/2009   331        Fri
34    12/5/2009   213        Sat
35    12/6/2009   444        Sun
36    12/7/2009   483        Mon
37    12/8/2009   225        Tue
38    12/9/2009   386        Wed
39   12/10/2009   102        Thu
40   12/11/2009   301        Fri
41   12/12/2009   375        Sat
42   12/13/2009   458        Sun
43   12/14/2009   332        Mon
44   12/15/2009   526        Tue
45   12/16/2009   515        Wed
46   12/17/2009   459        Thu
47   12/18/2009   312        Fri
48   12/19/2009   330        Sat
49   12/20/2009    34        Sun
50    1/16/2010    63        Sat
51    1/17/2010   238        Sun
52    1/19/2010    12        Tue
53    1/20/2010   481        Wed
54    1/21/2010   671        Thu
55    1/22/2010   439        Fri
56    1/23/2010   448        Sat
57    1/24/2010   648        Sun
58    1/25/2010   708        Mon
59    1/26/2010   695        Tue
60    1/27/2010   617        Wed
61    1/28/2010   499        Thu
62    2/25/2010   189        Thu
63    2/26/2010   551        Fri
64    2/27/2010   441        Sat
65    2/28/2010   716        Sun
66     3/1/2010   877        Mon
67     3/2/2010   758        Tue
68     3/3/2010   767        Wed
69     3/4/2010   721        Thu
70     3/5/2010   504        Fri
71     3/6/2010    36        Sat
72     4/5/2010   105        Mon
73     4/6/2010   885        Tue

1 个答案:

答案 0 :(得分:0)

我认为问题是基于您的which声明以及您如何使用for声明。 which()应该为星期日的行提供行索引(数字),因此它将是:1,6,13,19,...

您的for语句会为i提供相同的值,因此第一次循环会给您i=1,第二次会给您i=6等。

因此,当您测试if (i <= 2 && ...时,只有i的第一个值符合<=2要求,其余的将不会执行此部分测试。因此行19会遇到第一个else语句,即使它有星期五的数据。

也许第一个if应该具有i>2的条件,以便您忽略前两行?

*阅读评论后编辑*

在前两个if语句失败后,第19天点击:

if (i <= 2 && (dateCount.df$Date[i - 2] == dateCount.df$Date[i] - 2))

19大于2,因此不符合此要求。我认为您需要将其切换为i>=2。从

开始可能是最简单的
if( i<-2) {do something - This is when a Sunday if the first or 
           second day of the data}
else{
     if (dateCount.df$Date[i-1] == (dateCount.df$Date[i] - 1)).... etc

只需保留相同的代码,但删除所有部分,将i与2进行比较,然后再处理。