1

I am new to R, I want to calculate monthly and annual average of sea surface temperature (SST). I get an error in using aggregate function: "NO ROWS IN AGGREGATE". My data: NetCDF file, from November 2004 to May 2022. I have searched that no rows to aggregate could caused by the length of the vector that are not same, but after I try to equalize it, the result still the same. Thank you for your help. What I have done:

#opening file
library(ncdf4)
library(lubridate)
setwd("D:/Data analysis/SST_fg")
nc_fname <- "METOFFICE-GLO-SST-L4-REP-OBS-SST_1679654370682.nc"
nc_ds <- nc_open(nc_fname)

#extracting time and converting to date
dim_time <- ncvar_get(nc_ds, "time")
t_units <- ncatt_get(nc_ds, "time", "units")
t_ustr <- strsplit(t_units$value, " ")
t_dstr <- strsplit(unlist(t_ustr)[3], "-")
date <- ymd(t_dstr) + dseconds(dim_time)

#extracting sst and converting to celcius
suhu <- ncvar_get(nc_ds, "analysed_sst", collapse_degen=FALSE)
suhu = suhu - 273.15

#creating matrix for both time and sst
coords <- as.matrix(expand.grid(date))
temprtr <- as.matrix(expand.grid(suhu))

#equalizing length of vectors
maxlength <- max(length(coords), length(temprtr))
length(coords) <- maxlength
length(temprtr) <- maxlength

#creating dataframe
nc_df <- data.frame(cbind(coords, temprtr))

#calculating monthly average
monthlymean <- aggregate(nc_df$temprtr[!is.na(nc_df$temprtr)],
                         by = list(date = format(as.Date(nc_df$coords[!is.na(nc_df$temprtr)]),"%Y-%m")), mean, na.rm = TRUE)



Fitra
  • 9
  • 2
  • Welcome to SO. It is best to provide a code sample that is reproducible. Otherwise people have to guess what is causing the problem. Have you looked at the individuals args you are sending to aggregate? My guess is one of them has zero rows, as the error implies – Robert Wilson Mar 26 '23 at 08:07
  • Hi Robert, thanks for response. Is code sample NetCDF file that I use for my analyses? sorry for this stupid question. – Fitra Mar 26 '23 at 08:29
  • You have provided code. This should be something people can run in R themselves. People should be able to reproduce your problem – Robert Wilson Mar 26 '23 at 08:32
  • Hi Robert, I have edited the code that I used. I hope it can be reproduced by you and people. Please kindly have a look into my code. – Fitra Mar 26 '23 at 08:44
  • The code needs to be reproducible by someone else. They should be able to copy and paste it into R and run it. This is not true for your code – Robert Wilson Mar 26 '23 at 08:54

0 Answers0