Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

Find last date of previous week using data.table in R

$
0
0

I have a data.frame as shown below:

structure(list(Start_Date = c("D1", "D2", "D3", "D4", "NA", "NA", 
"D6", "D7"), Week = c("W1", "W1", "W1", "W2", "W2", "W3", "W4", 
"W4"), last_date = c(NA, NA, NA, "D3", "D3", "D4", "D4", "D4"
)), class = "data.frame", row.names = c(NA, -8L))

output is column 'last_date'

What I require ? -I want to find last non-NA date of previous week. If the previous week has only NA date then it should look into previous to previous week and find non-NA date. For example - for all dates of W2, the last date would be D3 (last non-NA date of previous week). For W3, it should return D4. For W4, as only last date of W3 is NA, it should look for non-NA date in previous to previous week (i.e. W2) and return D4.

Summarizing, the last date will be most recent non-NA date (not coming from current week)

As my data set is too bulky, I am looking for a data.table solution.


Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>