[R] How to select max data according to week?
SITI AISYAH BINTI ZAKARIA
@|@y@hz@k@r|@ @end|ng |rom un|m@p@edu@my
Tue Jun 18 07:58:11 CEST 2019
Hi,
I'm Aisyah..I have a problem to run my R coding. I want to select maximum value according to week.
here is my data
Date O3_Conc
20000101 0.033
20000102 0.023
20000103 0.025
20000104 0.041
20000105 0.063
20000106 0.028
20000107 0.068
20000108 0.048
20000109 0.037
20000110 0.042
20000111 0.027
20000112 0.035
20000113 0.063
20000114 0.035
20000115 0.042
20000116 0.028
I want to find the max value from column O3_Conc for only 14 days that refer to biweekly in month. And the next 14 days for the max value.
I hope that I can get the result like this:
Date Max O3_Conc
20000101 - 20000114 0.068
20000115 - 20000129 0.061
I try many coding but still unavailable.
this example my coding
library(plyr)
data.frame(CA0003)
# format weeks as per requirement (replace "00" with "52" and adjust corresponding year)
tmp <- list()
tmp$y <- format(df$Date, format="%Y")
tmp$w <- format(df$Date, format="%U")
tmp$y[tmp$w=="00"] <- as.character(as.numeric(tmp$y[tmp$w=="00"]) - 14)
tmp$w[tmp$w=="00"] <- "884"
df$week <- paste(tmp$y, tmp$w, sep = "-")
# get summary
df2 <- ddply(df, .(week),transform, O3_Conc=max(O3_Conc))
# include week ending date
tmp$week.ending <- lapply(df2$week, function(x) rev(df[df$week ==x, "O3_Conc"])[[1]])
df2$week.ending <- sapply(tmp$week.ending, max(O3_Conc, TRUE)
output
Site_Id Site_Location Date Year O3_Conc Month Day week
1 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000101 2000 0.033 1 1 NULL-NULL
2 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000102 2000 0.023 1 2 NULL-NULL
3 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000103 2000 0.025 1 3 NULL-NULL
4 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000104 2000 0.041 1 4 NULL-NULL
5 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000105 2000 0.063 1 5 NULL-NULL
6 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000106 2000 0.028 1 6 NULL-NULL
7 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000107 2000 0.068 1 7 NULL-NULL
8 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000108 2000 0.048 1 8 NULL-NULL
9 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000109 2000 0.037 1 9 NULL-NULL
10 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000110 2000 0.042 1 10 NULL-NULL
11 CA0003 Sek. Keb. Cederawasih, Taman Inderawasih, Perai 20000111 2000 0.027 1 11 NULL-NULL
--
This message has been scanned by E.F.A. Project and is believed to be clean.
More information about the R-help
mailing list