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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:01:22+00:00 2026-05-28T04:01:22+00:00

Here is probably an easy question.. but I am really struggling so help is

  • 0

Here is probably an easy question.. but I am really struggling so help is very much appreciated.

I have 4d data that I wish to transform into 3d data. The data has the following attributes:

lon <- 1:96  
lat <- 1:73  
lev <- 1:60  
tme <- 1:12

data <- array(runif(96*73*60*12), 
              dim=c(96,73,60,12) ) # fill with random test values  

What I would like to do is calculate the mean of the first few levels (say 1:6). The new data would be of the form:

new.data <- array(96*73*12), dim=c(96,73,12) ) # again just test data  

But would contain the mean of the first 5 levels of data. At the moment the only way I have been able to make it work is to write a rather inefficient loop which extracts each of the first 5 levels and divides the sum of those by 5 to get the mean.

I have tried:

new.data <- apply(data, c(1,2,4), mean)  

Which nicely gives me the mean of ALL the vertical levels but can’t understand how to subset the 3rd dimension to get an average of only a few! e.g.

new.data <- apply(data, c(1,2,3[1:5],4), mean) # which returns   
  Error in ds[-MARGIN] : only 0's may be mixed with negative subscripts

I am desperate for some help!

  • 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-28T04:01:23+00:00Added an answer on May 28, 2026 at 4:01 am

    apply with indexing (the proper use of “[“) should be enough for the mean of the first six levels of the third dimension if I understand your terminology:

    > str(apply(data[,,1:6,] , c(1,2,4), FUN=mean) )
     num [1:96, 1:73, 1:12] 0.327 0.717 0.611 0.388 0.47 ...
    

    This returns a 96 x 73 by 12 matrix.

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

Sidebar

Related Questions

I feel really dumb for asking this question, but here it goes. I have
This question is probably easy to solve, but here it comes. I would like
This is probably a really easy question to answer, but for some reason I'm
So this is probably a fairly easy question to answer but here goes anyway.
I have a tiny question, probably an easy one, but I couldn't found googling
I am probably overlooking something really simple here but I am trying to redirect
I'm probably missing something very obvious here. I have files with the extension .st
I have what is probably a really dumb grep in R question. Apologies, because
Pardon me, this really is a noob question but please understand that I do
I am new to Rails so this is probably an easy question but can't

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.