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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:29:26+00:00 2026-06-01T03:29:26+00:00

I a having a little trouble with vector or array operations. I have three

  • 0

I a having a little trouble with vector or array operations.

I have three 3D arrays and i wanna find the average of them. How can i do that? we can’t use mean() as it only returns a single value.

The more important is some of the cells in the arrays are NA whic mean if i just add them like

A = (B + C + D)/3 

The results of will show NA as well.

How can i let it recognise if the cell is NA then just skip it.

Like

 A = c(NA, 10, 15, 15, NA)
 B = c(10, 15, NA, 22, NA)
 C = c(NA, NA, 20, 26, NA)

I wanna the output of average these vectors be

(10, (10+15)/2, (15+20)/2, (15+22+26)/3, NA)

We also can’t use na.omit, because it will move the order of indexes.

This is the corresponding code. i wish it would be helpful.

for (yr in 1950:2011) {
    temp_JFM <- sst5_sst2[,,year5_sst2==yr & (month5_sst2>=1 & month5_sst2<=3)]
       k = 0
       jfm=4*k+1
    for (i in 1:72) {
        for (j in 1:36) {
            iposst5_sst2[i,j,jfm] <- (temp_JFM[i,j,1]+temp_JFM[i,j,2]+temp_JFM[i,j,3])/3
        }
    }      
}

Thnk you.

It already been solved.

The easiest way to correct it can be shown below.

iposst5_sst2[i,j,jfm] <- mean(temp_JFM[i,j,],na.rm=TRUE)
  • 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-01T03:29:27+00:00Added an answer on June 1, 2026 at 3:29 am

    Here’s an example which makes a vector of the three values, which makes na.omit usable:

    vectorAverage <- function(A,B,C) {
        Z <- rep(NA, length(A))
    
        for (i in 1:length(A)) {
            x <- na.omit(c(A[i],B[i],C[i]))
            if (length(x) > 0) Z[i] = mean(x)
        }
        Z
    }
    

    Resulting in:

    vectorAverage(A,B,C)
    [1] 10.0 12.5 17.5 21.0   NA
    

    Edited: Missed the NaN in the output of the first version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a little trouble overloading methods in C#. I have two methods that
I'm having a little trouble in understanding how I can read and return the
Im having a little trouble in referencing indexes in arrays in Smarty. I believe
Im having a little trouble setting up routes. I have a 'users' controller/model/views set
I'm having a little trouble defining my question. Here's what I have. A mailto
I am having a little trouble using addoperationwithblock in Cocoa. Let's say I have
I am having a little trouble understanding migrations in Ruby on Rails. I have
I am having a little trouble deciding how to structure an application that I
I'm having a little trouble with my OpenGL transformations -- I have a vertex
I am having a little trouble getting this to work. I have several anchor

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.