For a cycle - select a time window from the daily column

I need to set up a code that works fine with my data framework (but with a different setting) in order to select a 2-day time window from the Day column. In particular, I am interested in 1 day of the previous day 0 (i.e., I - 1 and i, where I am the day of interest), and its (i - 1) values ​​contained in the Count column should be added on day 0 (i) col Count.

Here is an example of my frame:

df <- read.table(text = "
        Station   Day           Count
    1    33012  12448               4
    2    35004  12448               4
    3    35008  12448               4
    4    37006  12448               4
    5    21009   4835               3
    6    24005   4835               3
    7    27001   4835               3
    8    25005  12447               3
    9    29001  12447               3
    10   29002  12447               3
    11   29002  12446               3
    12   30001  12446               3
    13   31002  12446               3
    14   47007   4834               2
    15   49002   4834               2
    16   47004  12445               1
    17   51001  12449               1
    18   51003   4832               1
    19   52004   4836               1", header = TRUE)

my conclusion should be:

           Station    Day           Count
        1    33012  12448               7
        2    35004  12448               7
        3    35008  12448               7
        4    37006  12448               7
        5    21009   4835               5
        6    24005   4835               5
        7    27001   4835               5
        8    29002  12446               4
        9    30001  12446               4
        10   31002  12446               4
        11   51001  12449               1
        12   51003   4832               1
        13   52004   4836               1
        14   25005  12447               0
        15   29001  12447               0
        16   29002  12447               0
        17   47007   4834               0
        18   49002   4834               0
        19   47004  12445               0

I am trying to use this code, but it does not work with my real data framework:

for (i in unique(df$Day)) {
    temp <- df$Count[df$Day == i]  
    if(length(temp > 0)) {  
    condition1 <- df$Day == i - 1   
    if (any(condition1)) {
       df$Count[df$Day == i] <- mean(df$Count[condition1]) + df$Count[df$Day == i]
       df$Count[condition1] <- 0
            }
         }
}

The code seems to be correct and it makes sense, but my conclusion is not.

Can anyone help me?


The @aichao code works well.

, 30 (, -30, -29, -28,...., -1, 0), , 30 ()?

@aichao .

+4
1

, ,

for (i in unique(df$Day)) {
  temp <- df$Count[df$Day == i]
  if (any(temp > 0)) {
    condition1 <- df$Day == i - 1
    condition1[which(df$Day == i - 1) < max(which(df$Day == i))] <- FALSE
    if (any(condition1)) {
      df$Count[df$Day == i] <- mean(df$Count[condition1]) + df$Count[df$Day == i]
      df$Count[condition1] <- 0
    }
  }
}
print(df[order(df$Count, decreasing = TRUE),])
##   Station   Day Count
##1    33012 12448     7
##2    35004 12448     7
##3    35008 12448     7
##4    37006 12448     7
##5    21009  4835     5
##6    24005  4835     5
##7    27001  4835     5
##11   29002 12446     4
##12   30001 12446     4
##13   31002 12446     4
##17   51001 12449     1
##18   51003  4832     1
##19   52004  4836     1
##8    25005 12447     0
##9    29001 12447     0
##10   29002 12447     0
##14   47007  4834     0
##15   49002  4834     0
##16   47004 12445     0

, , , , , ( ). , Day . df$Day = 12449 , , df$Day = 12448 . Count df$Day = 12449 1, , Counts , df$Day = 12448, df$Day = 12449.

, condition1, FALSE , df$Day == i - 1 ( ), , df$Day == i ( ),

condition1[which(df$Day == i - 1) < max(which(df$Day == i))] <- FALSE

, , Day , . for over unique(df$Day) , .

,

if(length(temp > 0)) {

, , - , Count , 0 . R , temp > 0 , , temp. length(temp > 0) , temp 0 (.. ). , ,

if(any(temp > 0)) {

:

- if (any(temp > 0)) {...} , accumulate.mean.count sapply. :

accumulate.mean.count <- function(this.day, lag) {
  condition1 <- df$Day == this.day - lag
  condition1[which(df$Day == this.day - lag) < max(which(df$Day == this.day))] <- FALSE
  if (any(condition1)) {
    df$Count[df$Day == this.day] <<- mean(df$Count[condition1]) + df$Count[df$Day == this.day]
    df$Count[condition1] <<- 0
  }
}

lags <- seq_len(30)

for (i in unique(df$Day)) {
  temp <- df$Count[df$Day == i]
  if (any(temp > 0)) {
    sapply(lags, accumulate.mean.count, this.day=i)
  }
}

print(df[order(df$Count, decreasing = TRUE),])

:

  • lag - , ( , ) . A lag = 1 , lag = 2 .. lags - . lags <- seq_len(30) 1 30, accumulate.mean.count, . . *apply R-. , lags , , c(1, 5, 10) , 5 10 . , , .

  • - R df$Count, accumulate.mean.count, accumulate.mean.count <<- <-. . <<-, .

lags <- seq_len(30), seq_len(1) , seq_len(2)

##   Station   Day Count
##1    33012 12448    10
##2    35004 12448    10
##3    35008 12448    10
##4    37006 12448    10
##5    21009  4835     5
##6    24005  4835     5
##7    27001  4835     5
##16   47004 12445     1
##17   51001 12449     1
##18   51003  4832     1
##19   52004  4836     1
##8    25005 12447     0
##9    29001 12447     0
##10   29002 12447     0
##11   29002 12446     0
##12   30001 12446     0
##13   31002 12446     0
##14   47007  4834     0
##15   49002  4834     0

, , .

+1

Source: https://habr.com/ru/post/1648009/


All Articles