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 6555743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:51:26+00:00 2026-05-25T12:51:26+00:00

I am trying to remove NA s from my data frame by interpolation with

  • 0

I am trying to remove NAs from my data frame by interpolation with na.approx() but can’t remove all of the NAs.

My data frame is a 4096×4096 with 270.15 as flag for non valid value. I need data to be continous in all points to feed a meteorological model. Yesterday I asked, and obtained an answer, on how to replace values in a data frame based in another data frame. But after that I came to na.approx() and then decided to replace the 270.15 values with NA and try na.approx() to interpolate data. But the question is why na.approx() does not replace all NAs.

This is what I am doing:

  • Read the original hdf file with hdf5load
  • Subset the data frame (4094×4096)
  • Substitute flag value with NA

    > sst4[sst4 == 270.15 ] = NA
    
  • Check first column (or any other)

    > summary(sst4[,1])
    
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
    271.3   276.4   285.9   285.5   292.3   302.8  1345.0
    
  • Run na.approx

    > sst4=na.approx(sst4,na.rm="FALSE")
    
  • Check first column

    > summary(sst4[,1]) 
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
    271.3   276.5   286.3   285.9   292.6   302.8   411.0
    

As you can see 411 NA’s have not been removed. Why? Do they all correspond to leading/ending column values?

head(sst4[,1])
[1] NA NA NA NA NA NA
tail(sst4[,1])
[1] NA NA NA NA NA NA

Is it needed by na.approx to have valid values before and after NA to interpolate? Do I need to set any other na.approx option?

Thank you very much

  • 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-05-25T12:51:27+00:00Added an answer on May 25, 2026 at 12:51 pm

    A small, reproducible example:

    library(zoo)
    set.seed(1)
    m <- matrix(runif(16, 0, 100), nrow = 4)
    missing_values <- sample(16, 7)
    m[missing_values] <- NA
    m
             [,1]     [,2]      [,3]     [,4]
    [1,] 26.55087 20.16819 62.911404 68.70228
    [2,] 37.21239       NA  6.178627 38.41037
    [3,]       NA       NA        NA       NA
    [4,] 90.82078 66.07978        NA       NA
    
    na.approx(m)
             [,1]     [,2]      [,3]     [,4]
    [1,] 26.55087 20.16819 62.911404 68.70228
    [2,] 37.21239 35.47206  6.178627 38.41037
    [3,] 64.01658 50.77592        NA       NA
    [4,] 90.82078 66.07978        NA       NA
    
    m[4, 4] <- 50
    na.approx(m)
             [,1]     [,2]      [,3]     [,4]
    [1,] 26.55087 20.16819 62.911404 68.70228
    [2,] 37.21239 35.47206  6.178627 38.41037
    [3,] 64.01658 50.77592        NA 44.20519
    [4,] 90.82078 66.07978        NA 50.00000
    

    Yup, looks like you do need the start/end values of columns to be known or the interpolation doesn’t work. Can you guess values for your boundaries?

    ANOTHER EDIT: So by default, you need the start and end values of columns to be known. However it is possible to get na.approx to always fill in the blanks by passing rule = 2. See Felix’s answer. You can also use na.fill to provide a default value, as per Gabor’s comment. Finally, you can interpolate boundary conditions in two directions (see below) or guess boundary conditions.


    EDIT: A further thought. Since na.approx is only interpolating in columns, and your data is spacial, perhaps interpolating in rows would be useful too. Then you could take the average.

    na.approx fails when whole columns are NA, so we create a bigger dataset.

    set.seed(1)
    m <- matrix(runif(64, 0, 100), nrow = 8)
    missing_values <- sample(64, 15)
    m[missing_values] <- NA
    

    Run na.approx both ways.

    by_col <- na.approx(m)
    by_row <- t(na.approx(t(m)))
    

    Find out the best guess.

    default <- 50
    best_guess <- ifelse(is.na(by_row), 
      ifelse(
        is.na(by_col), 
        default,              #neither known
        by_col                #only by_col known
      ), 
      ifelse(
        is.na(by_col), 
        by_row,               #only by_row known
        (by_row + by_col) / 2 #both known
      )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to remove all the rows from my data frame in which the
I'm trying to remove all but the first child component from a Java Container.
I'm trying to pull in data from a remote SQL Server. I can access
I am trying to remove all HTML elements from a String. Unfortunately, I cannot
I'm trying to create a player who can add and remove items from their
I am trying to remove a specific set of data from a MySQL database
I am trying to remove Details from http://localhost:1985/Materials/Details/2/Steel but some how my route doesn't
I am trying to remove duplicate data from my database. I found a nice
I'm trying to remove an object from (inside?) a object literal. But I cant
I'm trying find the best way to remove nonnumerical data from a varchar in

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.