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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:29:58+00:00 2026-05-31T23:29:58+00:00

I am having an issue with creating a splice for a weighted index. I

  • 0

I am having an issue with creating a splice for a weighted index. I have the following sample data:

a=(1:10)
b=(14:23)
c=rep(1,10)
wa=c(2,2,2,2,2,6,6,6,6,6)
wb=c(5,5,5,5,5,2,2,2,2,2)
wc=c(3,3,3,3,3,2,2,2,2,2)
z=data.frame(a,b,c,wa,wb,wc)
z$ind=rowSums(z[,1:3]*z[,4:6])/rowSums(z[,4:6])

Which returns the following data frame:

    a  b c wa wb wc  ind
1   1 14 1  2  5  3  7.5
2   2 15 1  2  5  3  8.2
3   3 16 1  2  5  3  8.9
4   4 17 1  2  5  3  9.6
5   5 18 1  2  5  3 10.3
6   6 19 1  6  2  2  7.6
7   7 20 1  6  2  2  8.4
8   8 21 1  6  2  2  9.2
9   9 22 1  6  2  2 10.0
10 10 23 1  6  2  2 10.8

The weights (wa,wb,wc) have changed at record six. So I would like to splice the index at record six so that 7.6 becomes 11. I need to calculate the values (a,b,c) with the previous record’s weights and divide that by 7.6. Then apply that to all following numbers until the weights change again. The following function allows me to find where one of my weights has changed:

changeWeight=function(x){
for(i in 2:NROW(z)) {
z$test[i] <- if(z$wa[i]-z$wa[i-1]==0) 0 else 1
}
z
}

It will return a value of one wherever the weight has changed like so:

    a  b c wa wb wc  ind test
1   1 14 1  2  5  3  7.5   NA
2   2 15 1  2  5  3  8.2    0
3   3 16 1  2  5  3  8.9    0
4   4 17 1  2  5  3  9.6    0
5   5 18 1  2  5  3 10.3    0
6   6 19 1  6  2  2  7.6    1
7   7 20 1  6  2  2  8.4    0
8   8 21 1  6  2  2  9.2    0
9   9 22 1  6  2  2 10.0    0
10 10 23 1  6  2  2 10.8    0

Now I try to create the value I will multiply by in order to splice the index at record six. I tried the following:

spliceValue=function(x){
for(i in 2:NROW(z)){
z$splice[i]=if(z$test[i]==1&z$splice[i-1]!=NA) (rowSums(z[i,1:3]*z[i-1,4:6])/rowSums(z[i-1,4:6]))/z$ind[i] else z$splice[i-1]
}
z
}

But that returns this error:

Error in if (z$test[i] == 1 & z$splice[i - 1] != NA) z$ind[i - 1]/z$ind[i] else z$splice[i -  : 
argument is of length zero

What I would like to get is this:

    a  b c wa wb wc  ind test   splice
1   1 14 1  2  5  3  7.5   NA       NA
2   2 15 1  2  5  3  8.2    0 0.000000
3   3 16 1  2  5  3  8.9    0 0.000000
4   4 17 1  2  5  3  9.6    0 0.000000
5   5 18 1  2  5  3 10.3    0 0.000000
6   6 19 1  6  2  2  7.6    1 1.447638
7   7 20 1  6  2  2  8.4    0 1.447638
8   8 21 1  6  2  2  9.2    0 1.447638
9   9 22 1  6  2  2 10.0    0 1.447638
10 10 23 1  6  2  2 10.8    0 1.447638

Then I can multiply ind by splice and have a nice smooth index.

  • 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-31T23:30:00+00:00Added an answer on May 31, 2026 at 11:30 pm

    Expanding the example to have more than one change in weights:

    a=(1:15)
    b=(14:28)
    c=rep(1,15)
    wa=c(2,2,2,2,2,6,6,6,6,6,5,5,5,5,5)
    wb=c(5,5,5,5,5,2,2,2,2,2,6,6,6,6,6)
    wc=c(3,3,3,3,3,2,2,2,2,2,3,3,3,3,3)
    z=data.frame(a,b,c,wa,wb,wc)
    z$ind=rowSums(z[,1:3]*z[,4:6])/rowSums(z[,4:6])
    

    Here, I have changed the functions changeWeight() and spliceValue() to return vectors that can be added to the data. This does what you want for the expanded example and avoids the environment issues that might occur with the original functions.

    changeWeight<-function(x){
      test <- NA
      for(i in 2:NROW(z)) {
        test[i] <- if(z$wa[i]-z$wa[i-1]==0) 0 else 1
      }
      return(test)
    }
    
    z$test<-changeWeight()
    

    The condition z$splice[i - 1]!=NA seemed superfluous. If it’s not, you should consider !is.na(z$splice[i - 1]) instead.

    spliceValue <- function(x) {
      splice <- 0
      for(i in 2:NROW(z)) {
        splice[i] <- if(z$test[i]==1) (rowSums(z[i,1:3]*z[i-1,4:6])/rowSums(z[i-1,4:6]))/z$ind[i] else splice[i-1]
      }
      return(splice)
    }
    z$splice<-spliceValue()
    

    And, as per the original example, to set the first value of z$splice to NA,

    z$splice[1]<-NA
    

    As a note, this approach may take a while if z has many rows.

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

Sidebar

Related Questions

I'm having the following issue with my hibernate 3.6.10 project: org.springframework.beans.factory.BeanCreationException: Error creating bean
i'm having an issue with creating a query in oracle which doesnt seem to
I am having an issue with creating a pop-up window. I have a game
Having an issue here that I have tried everything I can think of but
I am having an issue with creating and adding sales/quote_address objects to the multishipping
I am creating a modal window using jquery. I am having an issue with
I'm having an issue with a Struts 1 form, which contains a logic:iterate in
Well I'm having an issue with Facebook's Open Graph. I have a Wordpress blog
I'm having an issue creating my Oracle DB with schemaexport function of NHibernate. For
I'm having an issue with an creating application shortcut on the desktop from a

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.