1

A newbie question related to R. How do I extract time series data for a particular location using R from an netdcf file. So for example, the following snapshot shows that the time series for location (1,2) is 13,28,43.

Sample netcdf

Thanks in advance.

umbersar
  • 1,821
  • 3
  • 24
  • 34
  • 3
    Please, start with reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – zero323 Sep 16 '13 at 01:05
  • Clearly this data is in Excel (or similar) - how were you planning to import this into R and in what sort of representation? That will make a huge difference to potential answers. Screenshots of data are also practically useless for future reference. – thelatemail Sep 16 '13 at 01:32
  • Have you managed to get the netcdf data into R at all? Take a look at the `ncdf` package. – Scott Ritchie Sep 16 '13 at 01:41

2 Answers2

1

This might do it, where "my.variable" is the name of the variable you are interested in:

library(survival)
library(RNetCDF)
library(ncdf)
library(date)

setwd("c:/users/mmiller21/Netcdf")

my.data <- open.nc("my.netCDF.file.nc");

my.time <- var.get.nc(my.data, "time")

n.latitudes  <- length(var.get.nc(my.data, "lat"))
n.longitudes <- length(var.get.nc(my.data, "lon"))

n.dates <- trunc(length(my.time))
n.dates

my.object <- var.get.nc(my.data, "my.variable")

my.array  <- array(my.object, dim = c(n.latitudes, n.longitudes, n.dates))
my.array[,,1:5]
my.vector <- my.array[1, 2, 1:n.dates]  # first latitude, second longitude
head(my.vector)

baseDate <- att.get.nc(my.data, "NC_GLOBAL", "base_date")
bdInt    <- as.integer(baseDate[1])

year     <- date.mdy(seq(mdy.date(1, 1, bdInt), by = 1,
                     length.out = length(my.vector)))$year 

head(year)
Mark Miller
  • 12,483
  • 23
  • 78
  • 132
0
your_data <- read.csv("")

#Subsetting your data
location12 <- subset(your_data, latitude == 1 & column2_value == 2)

Data_location12 <- table(location12)

timeseries12 <- ts(Data_location12)

This should work.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99