测试按 posix 日期分组的变量中是否存在 n% 的数据值
Testing whether n% of data values exist in a variable grouped by posix date
我有一个数据框,其中包含多年的每小时观测气候数据,我在下面包含了一个虚拟数据框,希望能说明我的 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
我需要按年(或在本例中按月)分组,以确定 df$WS 是否有该月 75% 或更多的有效数据。我的过滤标准是 NA,因为 0 仍然是一个有效的观察值。我有真正的 NA,因为它是观测气候数据。
我已经尝试使用 %>% 函数的 dplyr 管道通过新列 "Month" 进行归档,并在此处查看了几个问题
Calculate the percentages of a column in a data frame - "grouped" by column,
Making a data frame of count of NA by variable for multiple data frames in a list,
None 个回答了我的问题。
我希望将一些东西放在一个更长的脚本中,该脚本在一个循环函数中工作,它将遍历我所有的站和每个站的所有年份,如果那一年/站满足这个标准,就会产生风向玫瑰.如果我需要澄清更多,请告诉我。
干杯
有很多方法可以做到这一点。这个看起来很有启发性。
首先创建一个表示月份的新变量(如果超过一年则表示年份)。拆分此变量并计算 NA 的数量。将其除以值的数量再乘以 100 得到百分比。
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
您也可以使用 data.table
.
library(data.table)
setDT(df)
df[, (sum(is.na(WS))/.N) * 100, by = 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
这是一个使用dplyr
的方法。即使您缺少数据,它也能正常工作。
library(lubridate) #for the days_in_month function
library(dplyr)
df2 <- df %>% mutate(Month=format(dateTime,"%Y-%m")) %>%
group_by(Month) %>%
summarise(No.Obs=sum(!is.na(WS)),
Max.Obs=24*days_in_month(as.Date(paste0(first(Month),"-01")))) %>%
mutate(Obs.Rate=No.Obs/Max.Obs)
df2
Month No.Obs Max.Obs Obs.Rate
<chr> <int> <dbl> <dbl>
1 2012-01 575 744 0.7728495
2 2012-02 545 696 0.7830460
3 2012-03 560 744 0.7526882
4 2012-04 537 720 0.7458333
5 2012-05 567 744 0.7620968
6 2012-06 557 720 0.7736111
7 2012-07 553 744 0.7432796
8 2012-08 568 744 0.7634409
9 2012-09 546 720 0.7583333
10 2012-10 544 744 0.7311828
11 2012-11 546 720 0.7583333
12 2012-12 554 744 0.7446237
我有一个数据框,其中包含多年的每小时观测气候数据,我在下面包含了一个虚拟数据框,希望能说明我的 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
我需要按年(或在本例中按月)分组,以确定 df$WS 是否有该月 75% 或更多的有效数据。我的过滤标准是 NA,因为 0 仍然是一个有效的观察值。我有真正的 NA,因为它是观测气候数据。
我已经尝试使用 %>% 函数的 dplyr 管道通过新列 "Month" 进行归档,并在此处查看了几个问题
Calculate the percentages of a column in a data frame - "grouped" by column,
Making a data frame of count of NA by variable for multiple data frames in a list,
None 个回答了我的问题。
我希望将一些东西放在一个更长的脚本中,该脚本在一个循环函数中工作,它将遍历我所有的站和每个站的所有年份,如果那一年/站满足这个标准,就会产生风向玫瑰.如果我需要澄清更多,请告诉我。 干杯
有很多方法可以做到这一点。这个看起来很有启发性。
首先创建一个表示月份的新变量(如果超过一年则表示年份)。拆分此变量并计算 NA 的数量。将其除以值的数量再乘以 100 得到百分比。
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
您也可以使用 data.table
.
library(data.table)
setDT(df)
df[, (sum(is.na(WS))/.N) * 100, by = 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
这是一个使用dplyr
的方法。即使您缺少数据,它也能正常工作。
library(lubridate) #for the days_in_month function
library(dplyr)
df2 <- df %>% mutate(Month=format(dateTime,"%Y-%m")) %>%
group_by(Month) %>%
summarise(No.Obs=sum(!is.na(WS)),
Max.Obs=24*days_in_month(as.Date(paste0(first(Month),"-01")))) %>%
mutate(Obs.Rate=No.Obs/Max.Obs)
df2
Month No.Obs Max.Obs Obs.Rate
<chr> <int> <dbl> <dbl>
1 2012-01 575 744 0.7728495
2 2012-02 545 696 0.7830460
3 2012-03 560 744 0.7526882
4 2012-04 537 720 0.7458333
5 2012-05 567 744 0.7620968
6 2012-06 557 720 0.7736111
7 2012-07 553 744 0.7432796
8 2012-08 568 744 0.7634409
9 2012-09 546 720 0.7583333
10 2012-10 544 744 0.7311828
11 2012-11 546 720 0.7583333
12 2012-12 554 744 0.7446237