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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:34:41+00:00 2026-06-02T16:34:41+00:00

I have large vector with the following type: myvec <- c(1,2,3,5, 0,1,2,5,8, 0,1,3, 0,2,3,8,

  • 0

I have large vector with the following type:

myvec <- c(1,2,3,5,  0,1,2,5,8,  0,1,3,  0,2,3,8,  0,3,5)

The corresponding vector for group is:

grp <- c(rep(1, 4), rep(2, 5), rep(3,3), rep(4, 4), rep(5, 3))

I need to add the number such way that the last number of consecutive group will be added to the distance in second group plus 2 (to create a gap). Then last number of second group will added to third pus 3 to create a gap.

thus new vector will look like

       myvecnew <- c(1,  2, 3 ,5,  # maximum 5 + 2 applied in group 2

     5+0+2 = 7, 5+1+2 = 8, 5+2+2 = 9, 5+5+2 = 12, 8+5+2 = 15, # maximum 15 + 2 applied in group 3

       0+15+2 = 17, 1+15+2 = 18, 3+15+2 = 20, # maximum 20 + 2 applied to group 4 

                  0 + 20 + 2 = 22,..........and so on)

Thus max(value) of group 1 plus 2 is added to every group 2 values, the resulting new maximum for group 2 will be added to group 3 plus the constant 2, and I need to keep going till all groups are covered.
How can this be achieved? ……..

  • 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-02T16:34:43+00:00Added an answer on June 2, 2026 at 4:34 pm

    Reading through what I think are 2 errors in the question (see comments), I believe you can achieve what you want like:

    grps   = lapply(unique(grp), function(x) myvec[grp==x])
    to.add = cumsum(sapply(grps, max) + 2)
    
    > c(grps[[1]], unlist(lapply(2:length(grps), function(x) grps[[x]] + to.add[x-1])))
     [1]  1  2  3  5  7  8  9 12 15 17 18 20 22 24 25 30 32 35 37
    

    (As you can see, I would also prefer to work with lists)

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

Sidebar

Related Questions

I have a very large std::vector of std::vectors containing a fixed number of unsigned
I have a large vector (10^9 elements) of chars, and I was wondering what
I have a large nested vector that look like this: import Data.Vector let x
I have large set will a number of variables: set.seed (14) pool = sample
My application problem is the following - I have a large structure foo. Because
I have a large number of equations (n) with a large number of unknowns
Total newbie to R here (so far very pleased!). I have a large array(vector?)
I have the following program to remove even numbers from a string vector, when
Let's say I have to iterate over a potentially very large vector of numbers
I have a very large multidimensional vector that changes in size all the time.

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.