r - Testing whether n% of data values exist in a variable grouped by posix date -
i have data frame has hourly observational climate data on multiple years, have included dummy data frame below illustrate qu.
datetime <- seq(as.posixct("2012-01-01"), as.posixct("2012-12-31"), by=(60*60)) ws <- sample(0:20,8761,rep=true) wd <- sample(0:390,8761,rep=true) temp <- sample(0:40,8761,rep=true) df <- data.frame(datetime,ws,wd,temp) df$ws[ws>15] <- na
i need group year (or in example, month) find if df$ws has 75% or more of valid data month. filtering criteria na 0 still valid observation. have real nas observational climate data.
i have tried dplyr piping using %>% function filer new column "month" reviewing several questions on here calculate percentages of column in data frame - "grouped" column, making data frame of count of na variable multiple data frames in list, r group date, , summarize values
none of these have answered question.
my hope put in longer script works in looping function go through stations , years in each station produce wind rose if criteria met year / station. please let me know if need clarify more. cheers
there many way of doing this. 1 appears quite instructive.
first create new variable denote month (and account year if have more 1 year). split on variable , count number of nas. divide number of values , multiply 100 percentage points.
df$monthyear <- format(df$datetime, format = "%m %y") out <- split(df, f = df$monthyear) sapply(out, function(x) (sum(is.na(x$ws))/nrow(x)) * 100) 01 2012 02 2012 03 2012 04 2012 05 2012 06 2012 07 2012 23.92473 21.40805 24.09152 25.00000 20.56452 24.58333 27.15054 08 2012 09 2012 10 2012 11 2012 12 2012 22.31183 25.69444 23.22148 21.80556 24.96533
you use data.table
.
library(data.table) setdt(df) df[, (sum(is.na(ws))/.n) * 100, = monthyear] monthyear v1 1: 01 2012 23.92473 2: 02 2012 21.40805 3: 03 2012 24.09152 4: 04 2012 25.00000 5: 05 2012 20.56452 6: 06 2012 24.58333 7: 07 2012 27.15054 8: 08 2012 22.31183 9: 09 2012 25.69444 10: 10 2012 23.22148 11: 11 2012 21.80556 12: 12 2012 24.96533
Comments
Post a Comment