简体   繁体   English

使用汇总求和的R中的值大于70

[英]Using aggregate to sum values greater than 70 in R

I am trying to sum values that are greater than 70 in several different data sets. 我试图在多个不同的数据集中求和大于70的值。 I believe that aggregate can do this but my research has not pointed to an obvious solution to obtaining the values that exceed seventy in my data sets. 我相信聚合可以做到这一点,但是我的研究并未指出一种明显的解决方案来获取我的数据集中超过70的值。 I have first used aggregate to get the daily max values and put these values into the data frame called yearmaxs. 我首先使用聚合来获取每日最大值,并将这些值放入称为Yearmaxs的数据框中。 Here is my code and what I have tried: 这是我的代码以及我尝试过的内容:

number of times O3 >70 in a year per site 每个站点一年中O3大于70的次数

Sys.setenv(TZ = "UTC")
library(openair)
library(lubridate)
filedir <- "C:/Users/dfmcg/Documents/Thesisfiles/8hravg"
myfiles <- c(list.files(path = filedir))
paste(filedir, myfiles, sep = '/')
npsfiles <- c(paste(filedir, myfiles,sep = '/'))

for (i in npsfiles[22]) {

  x <- substr(i,45,61)
  y <- paste('C:/Users/dfmcg/Documents/Thesisfiles/exceedenceall', x, sep='/')
  timeozone <- import(i, date="DATES", date.format = "%Y-%m-%d %H", header=TRUE, na.strings="NA")

  overseventy <- c()
  yearmaxs <- aggregate(rolling.O3new ~ format(as.Date(date)), timeozone, max)
  colnames(yearmaxs) <- c("date", "daymax")
  overseventy <- aggregate(daymax ~ format(as.Date(date)), yearmaxs, FUN = length,
        subset = as.numeric(daymax) > 70)
  colnames(overseventy) <- c("date", "daymax")
  aggregate(daymax ~ format(as.Date(date), "%Y"), overseventy, sum)

I have also tried: sum > "70 and sum(daymax > "70). 我也尝试过:sum>“ 70 and sum(daymax>” 70)。

My other idea at this point is using a for loop to iterate through the values. 我此时的另一个想法是使用for循环遍历值。 I was hoping that a could use aggregate again to sum the values of interest. 我希望a可以再次使用聚合来求和感兴趣的值。 Any help at all would be greatly appreciated! 任何帮助将不胜感激!

I think you want: 我想你要:

aggregate(daymax ~ format(as.Date(date)), yearmaxs, FUN = length,
          subset = as.numeric(daymax) > 70)

To things: 要事:

  1. you need numerical comparison, so use as.numeric(daymax) > 70 not daymax > "70" ; 您需要进行数值比较,因此请使用as.numeric(daymax) > 70而不是daymax > "70" ;
  2. use the subset argument in aggregate.formula . aggregate.formula使用subset参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM