Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

R: Extract/mark the first existing date and last which is largest date among 20th-22nd per each month, out of a list of dates over years


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

library(tidyverse)
aux_sim$date <- as.Date(aux_sim$date, "%d/%m/%Y")

# create columns day, month, year
aux_sim <- aux_sim %>% 
  arrange(date) %>% 
  mutate(d= day(date), m=month(date), y=year(date))

# extract first row of each month, year
a <- aux_sim %>%
  group_by(m, y) %>% 
  slice(1)

# extract the last-2 row of each month, year with day<=25
b <- aux_sim %>% 
  group_by(m, y) %>% 
  filter(d<=25) %>% 
  slice(n()-2)

rbind(a, b) %>% arrange(date) %>% select(date, spot.price.Crude.oil)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...