0

I would like to create a new column "test" according to dates calendar. If duration = n I put the same value for temp test=temp corresponding to n-1 days before dates

  Ozone Solar.R Wind Temp Month Day duration Year      Dates  MaxWind MaxDates
 41     190  7.4   67     5   1        1 1900 1900-05-01     7.4  1900-05-01  
 36     118  8.0   72     5   2       NA 1900 1900-05-02      NA          NA
 12     149 12.6   74     5   3        0 1900 1900-05-03      NA          NA
 18     313 11.5   62     5   4        2 1900 1900-05-04     14.3 1900-05-05
 NA      NA 14.3   56     5   5        0 1900 1900-05-05      NA          NA
 28      NA 14.9   66     5   6        0 1900 1900-05-06      NA          NA 
 23     299  8.6   65     5   7        3 1900 1900-05-07     20.1 1900-05-09
 19      99 13.8   59     5   8        0 1900 1900-05-08      NA          NA
  8      19 20.1   61     5   9       NA 1900 1900-05-09      NA          NA   

Sample data -

structure(list(ozone = c(41, 36, 12, 18, NA, 28, 23, 19, 8), 
    Solar.R = c(190, 118, 149, 313, NA, NA, 299, 99, 19), Wind = c(7.4, 
    8, 12.6, 11.5, 14.3, 14.9, 8.6, 13.8, 20.1), Temp = c(67, 
    72, 74, 62, 56, 66, 65, 59, 61), Month = c(5, 5, 5, 5, 5, 
    5, 5, 5, 5), Day = 1:9, duration = c(1, NA, 0, 2, 0, 0, 3, 
    0, NA), Year = c(1900, 1900, 1900, 1900, 1900, 1900, 1900, 
    1900, 1900), Dates = structure(1:9, .Label = c("1900-05-01", 
    "1900-05-02", "1900-05-03", "1900-05-04", "1900-05-05", "1900-05-06", 
    "1900-05-07", "1900-05-08", "1900-05-09"), class = "factor"), 
    MaxWind = c(7.4, NA, NA, 14.3, NA, NA, 20.1, NA, NA), MaxDates = c(1894, 
    NA, NA, 1890, NA, NA, 1886, NA, NA)), class = "data.frame", row.names = c(NA, 
-9L))
Suhas Hegde
  • 366
  • 1
  • 6
  • 13
Milo_R
  • 29
  • 8
  • What if `duration == 0`? Then `n - 1 == -1` and you will get a `Dates` after not before. – Rui Barradas Aug 24 '18 at 18:33
  • Please share the code you're working on and your data using `dput()` (not `str` or `head` or picture/screenshot) so others can help. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Aug 24 '18 at 18:33

1 Answers1

1

If I understand the question correctly, the following code will do what you need.

colTest <- function(DF, n, col = "test"){
    if(is.null(DF[[col]])) DF[[col]] <- NA
    i <- which(DF[["duration"]] == n)
    j <- which(DF[["Dates"]] %in% (DF[["Dates"]][i] - (n - 1)))
    DF[[col]][j] <- DF[["Temp"]][i]
    DF
}

colTest(df1, 0)
colTest(df1, 1)
colTest(df1, 2)
colTest(df1, 3)

Note that it will also work for n = 0.

Data.

df1 <- read.table(text = "
  Ozone Solar.R Wind Temp Month Day duration Year      Dates  MaxWind MaxDates
 41     190  7.4   67     5   1        1 1900 1900-05-01     7.4  1900-05-01  
 36     118  8.0   72     5   2       NA 1900 1900-05-02      NA          NA
 12     149 12.6   74     5   3        0 1900 1900-05-03      NA          NA
 18     313 11.5   62     5   4        2 1900 1900-05-04     14.3 1900-05-05
 NA      NA 14.3   56     5   5        0 1900 1900-05-05      NA          NA
 28      NA 14.9   66     5   6        0 1900 1900-05-06      NA          NA 
 23     299  8.6   65     5   7        3 1900 1900-05-07     20.1 1900-05-09
 19      99 13.8   59     5   8        0 1900 1900-05-08      NA          NA
  8      19 20.1   61     5   9       NA 1900 1900-05-09      NA          NA
", header = TRUE)

df1$Dates <- as.Date(df1$Dates)
df1$MaxDates <- as.Date(df1$MaxDates)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66