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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:20:25+00:00 2026-06-14T17:20:25+00:00

I average coordinates stored in a data frame as follows: sapply(coords[N:M,],mean) # mean of

  • 0

I average coordinates stored in a data frame as follows:

sapply(coords[N:M,],mean) # mean of coordinates N to M

I need the average of several sets of coordinates, so I made this loop, which finds the mean of coordinates 1-4, 5-11 and 20-30.

N <- c(1, 5,20)
M <- c(4,11,30)
for ( i in 1:length(N) ) {
    sapply(coords[N(i):M(i),],mean)
}

How can I vectorize that loop? I’ve tried to pass a matrix to coords (coords[NM,]), but that doesn’t give me what I want.

  • 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-14T17:20:26+00:00Added an answer on June 14, 2026 at 5:20 pm

    You may replace your sapply(x, mean) by colMeans(x) in the sake of simplicity and efficiency.

    Perhaps by a vector thinking you prefer to convert several variables (N and M) to a single vector – here array – when possible and simple.

    N <- data.frame(from=c(1,5,20), to=c(4,11,30))
    apply(N, 1, function(x) colMeans(coords[x[1]:x[2],]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've plotted a moving average from about 300,000 data points and I need to
I want to average some .jpg images which are corrupted by zero-mean Gaussian additive
I need to average some values in a row -wise fashion, rather than a
The arithmetic mean or average is easy to implement since it's often integrated as
The average-case analysis is quite hard to do for disjoint data structure. The least
I'd like to average the data However, length of the data is different each
I would like to average pairs of columns in a data set, not with
On average, how long does it take for Debian to package new versions of
I am an average newbie to CakePHP. While reading cookbook found Ajax form and
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload

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.