Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:05:39+00:00 2026-06-12T11:05:39+00:00

I have animal gps data with several locations for each day and regular gaps

  • 0

I have animal gps data with several locations for each day and regular gaps of several days when no location of the animal was recorded. Additionally, I have satellite data in intervals of 16 days. I would now like to extract the pixel value that corresponds to the specific point and to the specific time.

This means if the location of the animals was recorded e.g. 2 days before a satellite image was taken, I want to extract the pixel value of this image (that comes after) rather than from the images that was taken 14 days before the animal location was recorded. I always want to extract from the image that lies closer according to the time.

I created some test data that hopefully illustrates the problem:

library(sp)
library(raster)

### Create test data
# create first raster
edc2012001_m <- raster(ncol=36, nrow=18)
edc2012001_m[] <- sample(1:ncell(edc2012001_m))

# create second raster
edc2012017_m <- raster(ncol=36, nrow=18)
edc2012017_m[] <- sample(1:ncell(edc2012017_m))

rasters<-stack(edc2012001_m,edc2012017_m)

# Create xy coordinates
time<-c("2012-01-01", "2012-01-01", "2012-01-01", "2012-01-02", "2012-01-02", "2012-01-02", "2012-01-12", "2012-01-12", "2012-01-13", "2012-01-13")
x <- rep(-50,10)
y <- sample(c(-80:80), 10)
data<-data.frame(x,y,time)

# Convert data to spatial points data frame
coordinates(data) <- c("x","y")

### Extract all data from raster stack
extract(rasters, data)

The number string in the name of the first raster indicates that the image was taken on the first day in 2012, the second image was taken on the 17th day of this year.

The 7th location in the test data for example shall now be extracted from the second raster file as it lies closer according to the time.

All in all I have 87 raster files and 600 observations. I am really not sure how to program this. I guess I could use substr() to retrieve the date info from the raster names. But besides that…
I am thankful for every hint I can get, also about functions that might be helpful in this context.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:05:40+00:00Added an answer on June 12, 2026 at 11:05 am

    Starting from your last line

    ### Extract all data from raster stack
    ex <- extract(rasters, data)
    
    # assuming you have these names or something similar
    x <- c("edc2012001_m", "edc2012017_m")
    year <- as.integer(substr(x, 4, 7))
    # day of year
    doy <- as.integer(substr(x, 8, 10))
    date <- as.Date(doy, origin=paste(year-1, "-12-31", sep=''))
    time <- as.Date(time)
    
    # time difference
    dif <- t(apply(matrix(as.integer(time)), 1, function(x) x-as.integer(date)))
    
    # smallest time difference
    i <- apply(abs(dif), 1, which.min)
    
    # combine rows and columns to select values
    v <- ex[cbind(1:nrow(ex),i)]
    

    I get

    > i
     [1] 1 1 1 1 1 1 2 2 2 2
    > v
     [1] 582 578 303 201 201 200 461 329 445 211
    > 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weighted graph of multiple animal pens with each pen having at
Consider this example (typical in OOP books): I have an Animal class, where each
I have two grids: Animal Location. Location has foreign key AnimalId . After I
I have a class structure like abstract class Animal { public Animal(){ //init stuff..
say I have this class: class animal { function noise() { print 'woof'; }
I have this example: public class Inheritance { public static class Animal { public
I have three lists, I need to move Animal object from list animalSource to
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have an enum, which looks like public enum Animal { ELEPHANT, GIRAFFE, TURTLE,
If I have a superclass, say Animal , and two subclasses: Zebra and Giraffe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.