0

I have downloaded an NC file with long term (1992-01-01 to 2016-12-31) temperature data I want to extract the mean monthly and yearly temperatures using R into a spatial TIFF file so that I can then extract the mean monthly temperature data in an Excel dataset I have, but I have no idea how. The data can be accessed from here.

If someone could help me with the coding that would be appriciated.

With this code I tried to open and view the data:

library(ncdf4)
library(raster)
temp <- nc_open("dataset-ibi-reanalysis-phys-005-002-monthly_1547718004288.nc",write=TRUE)


TEMP = ncvar_get(temp,"bottomT")

latitude = ncvar_get(temp,"latitude")
longitude = ncvar_get(temp,"longitude")
nc_close(temp)
5
  • 1
    Could you show a sample of the data using dput to get alternative approaches independent of these packages(if possible)? Commented Jan 28, 2019 at 16:24
  • So i have approximately 17,000 spatial data points dating from 2003 to 2018 with numerous spatial points overlapping each other, data points for most days of the year, but some days I have more data points than other. The latitude minimum 43.38080 and maximum is 51.09567 and the longitude minimum -6.93330 and maximum is 1.88117. I'll try and figure our dput. Sorry I'm not aware of this function.. Commented Jan 29, 2019 at 11:01
  • Add the output of head(yourdata) for a more visual picture of what it looks like. The dput might be less useful but still add it using dput(head(df,20)) Commented Jan 29, 2019 at 11:04
  • No, Just copy and paste that. Use dput Commented Jan 29, 2019 at 14:49
  • Sorry it's too large.This might help: 'data.frame': 17070 obs. of 6 variables: $ trpCode: int 773321 773321 773321 773322 773322 773341 773341 773341 773361 773361 ... $ staNum : int 1 2 3 1 3 2 4 5 2 3 ... $ latIni : num 47.2 47.2 47.2 47.3 47.3 ... $ lonIni : num -2.73 -2.77 -2.77 -2.63 -2.67 ... $ date : Factor w/ 2934 levels "2003-02-15","2003-02-16",..: 1091 1091 1091 1118 1118 931 931 932 1108 1109 ... $ PA : int 0 0 0 0 0 0 0 0 0 0 ... Commented Jan 29, 2019 at 15:00

1 Answer 1

0

If you are able to create a data.frame that has date and temperature columns, similar to my dummy dataset below, you can easily use the dplyr and lubridate packages to get monthly and yearly means.

Add Month and Year Columns to df using dplyr's mutate function and lubridates month and year functions

library(lubridate)
library(dplyr)
df <- data.frame(date = seq(from = ymd("2017-01-01"), to = ymd("2018-12-31"), length.out = 730),
                 temp = rnorm(n = 730, mean = 23, sd = 12))

Add Month and Year Columns to df using dplyr's mutate function and lubridate's month and year functions

df <- df %>% mutate(Month = month(date), Year = year(date))

The first six rows of df:

# date     temp         Month Year
# 2017-01-01 17.13885     1 2017
# 2017-01-02 34.23553     1 2017
# 2017-01-03 10.25110     1 2017
# 2017-01-04 11.19415     1 2017
# 2017-01-05 28.09097     1 2017
# 2017-01-06 17.58424     1 2017

You can get the monthly means using dplyr's group_by function, to group the data.frame by month and year, and the summarise function to calculate the means.

monthly_summaries <- df %>% group_by(Year, Month) %>% 
  summarise(mean_temp = mean(temp))

Here's the first 6 rows of the monthly_summaries data.frame

#   Year Month mean_temp
#  2017     1      22.1
#  2017     2      24.6
#  2017     3      20.5
#  2017     4      25.7
#  2017     5      21.3
#  2017     6      23.4

Similarly, to get the yearly means, using the group_by function to group by year, and the summarise function to calculate the yearly means.

yearly_summaries <- df %>% group_by(Year) %>% 
  summarise(mean_temp = mean(temp))

And here's the yearly summaries data.frame:

#Year    mean_temp
#  2017      23.0
#  2018      23.1

(alternatively, if you don't want to add Month and Year columns to your data.frame, you could get the same output using the following code:

monthly_summaries <- df %>% group_by(Year = year(date), Month = month(date)) %>% 
    summarise(mean = mean(temp))
yearly_summaries <- df %>% group_by(Month = month(date)) %>% 
    summarise(mean = mean(temp))

)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this handy summary coding. However, my issue is extracting the temperature data from the nc file to my data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.