嵌套if中的逻辑问题在R中?

嵌套if中的逻辑问题在R中?,r,if-statement,nested,R,If Statement,Nested,我从名为dateCount.df的数据框中获得了以下数据(见下文) 你可以看到第19条记录是星期天,上一条记录是星期五。我试图编写一个嵌套的if-else语句,它将取星期五、星期六和星期天的nrec字段值,并对它们进行平均,然后用该平均值替换星期天的nrec。它不适用于记录19。有人能发现我的嵌套逻辑中的错误吗 suns = which(format(dateCount.df$Date, "%w") == 0) for (i in suns) { # this first "if" s

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

你可以看到第19条记录是星期天,上一条记录是星期五。我试图编写一个嵌套的if-else语句,它将取星期五、星期六和星期天的nrec字段值,并对它们进行平均,然后用该平均值替换星期天的nrec。它不适用于记录19。有人能发现我的嵌套逻辑中的错误吗

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]
                }
            }
        }
    }
}

我认为这些问题是基于您的
哪个
声明以及您如何使用
for
声明。
which()

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


所以,当你测试
if(i)时,当你输入一个问题时,会有一个预览,这样你可以在发帖前看到它是否看起来像胡言乱语。如果可能的话,请编辑它来修复它。所以你是说记录19应该改为(238+301)/2,记录6应该改为(212+238+242)/3?但是星期五和星期六的值保持不变?你能提供你期望的输出吗?这会有帮助。嗨。是的,第19行的值应该是(238+301)/2。第6行的值应该是(212+238+424)/3我认为第19行不应该通过第一个标准,因为它不符合这部分:dateCount.df$Date[i-1]=(dateCount.df$Date[i]-1)。因此,它将进入第一个else块。然后,由于相同的原因,它将不满足下一个if语句,因此它将进入下一个else块。在我看来,它应该通过下一个if测试:I>=2&(dateCount.df$Date[I-2]==dateCount.df$Date[I]-2)。因此,赋值应该是: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
if (i <= 2 && (dateCount.df$Date[i - 2] == dateCount.df$Date[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