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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:48:27+00:00 2026-06-10T11:48:27+00:00

I haven’t found something which precisely matches what I need, so I thought I’d

  • 0

I haven’t found something which precisely matches what I need, so I thought I’d post this.
I have a number of functions which basically rely on a rolling index of a variable, with a function, and should naturally flow back into the dataframe they came from.

For example,

data<-as.data.frame(as.matrix(seq(1:30)))
data$V1<-data$V1/100
str(data)

data$V1<-NA  # rolling 5 day product
for (i in 5:nrow(data)){
  start<-i-5
  end<-i
  data$V1_MA5d[i]<- (prod(((data$V1[start:end]/100)+1))-1)*100
}
data
> head(data,15)
     V1   V1_MA5d
1  0.01        NA
2  0.02        NA
3  0.03        NA
4  0.04        NA
5  0.05 0.1500850
6  0.06 0.2101751
7  0.07 0.2702952
8  0.08 0.3304453
9  0.09 0.3906255
10 0.10 0.4508358
11 0.11 0.5110762
12 0.12 0.5713467
13 0.13 0.6316473
14 0.14 0.6919780
15 0.15 0.7523389

But really, I should be able to do something like:

data$V1_MA5d<-sapply(data$V1, function(x) prod(((data$V1[i-5:i]/100)+1))-1)*100

But I’m not sure what that would look like.

Likewise, the count of a variable by another variable:

data$V1_MA5_cat<-NA
data$V1_MA5_cat[data$V1_MA5d<.5]<-0
data$V1_MA5_cat[data$V1_MA5d>.5]<-1
data$V1_MA5_cat[data$V1_MA5d>1.5]<-2
table(data$V1_MA5_cat)

data$V1_MA5_cat_n<-NA
data$V1_MA5_cat_n[data$V1_MA5_cat==0]<-nrow(subset(data,V1_MA5_cat==0))
data$V1_MA5_cat_n[data$V1_MA5_cat==1]<-nrow(subset(data,V1_MA5_cat==1))
data$V1_MA5_cat_n[data$V1_MA5_cat==2]<-nrow(subset(data,V1_MA5_cat==2))

> head(data,15)
     V1   V1_MA5d V1_MA5_cat V1_MA5_cat_n
1  0.01        NA         NA           NA
2  0.02        NA         NA           NA
3  0.03        NA         NA           NA
4  0.04        NA         NA           NA
5  0.05 0.1500850          0            6
6  0.06 0.2101751          0            6
7  0.07 0.2702952          0            6
8  0.08 0.3304453          0            6
9  0.09 0.3906255          0            6
10 0.10 0.4508358          0            6
11 0.11 0.5110762          1           17
12 0.12 0.5713467          1           17
13 0.13 0.6316473          1           17
14 0.14 0.6919780          1           17
15 0.15 0.7523389          1           17

I know there is a better way – 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-06-10T11:48:28+00:00Added an answer on June 10, 2026 at 11:48 am

    You can do this one of a few ways. Its worth mentioning here that you did write a “correct” for loop in R. You preallocated the vector by assigning data$V1_MA5d <- NA. This way you are filling rather than growing and its actually fairly efficient. However, if you want to use the apply family:

    sapply(5:nrow(data), function(i) (prod(data$V1[(i-5):i]/100 + 1)-1)*100)
    
    [1] 0.1500850 0.2101751 0.2702952 0.3304453 0.3906255 0.4508358 0.5110762 0.5713467 0.6316473 0.6919780 0.7523389 0.8127299
    [13] 0.8731511 0.9336024 0.9940839 1.0545957 1.1151376 1.1757098 1.2363122 1.2969448 1.3576077 1.4183009 1.4790244 1.5397781
    [25] 1.6005622 1.6613766
    

    Notice my code inside the [] is different from yours. check out the difference:

    i <- 10
    i - 5:i
    (i-5):i
    

    Or you can use rollapply from the zoo package:

    library(zoo)
    myfun  <- function(x) (prod(x/100 + 1)-1)*100
    rollapply(data$V1, 5, myfun)
    
    [1] 0.1500850 0.2001551 0.2502451 0.3003552 0.3504853 0.4006355 0.4508057 0.5009960 0.5512063 0.6014367 0.6516872 0.7019577
    [13] 0.7522484 0.8025591 0.8528899 0.9032408 0.9536118 1.0040030 1.0544142 1.1048456 1.1552971 1.2057688 1.2562606 1.3067726
    [25] 1.3573047 1.4078569
    

    As per the comment, this will give you a vector of length 26… instead you can add a few arguments to rollapply to make it match with your initial data:

    rollapply(data$V1, 5, myfun, fill=NA, align='right')
    

    In regard to your second question, plyr is handy here.

    library(plyr)
    data$cuts <- cut(data$V1_MA5d, breaks=c(-Inf, 0.5, 1.5, Inf))
    ddply(data, .(cuts), transform, V1_MA5_cat_n=length(cuts))
    

    But there are many other choices too.

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

Sidebar

Related Questions

Haven't found a solution to this problem yet, have tried some things... I have
I haven't seen this question anywhere else, I hope someone can help. I have
I haven't found any documentation on this or seen this done before, but is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I haven't found a definite answer to this yet. Lots of apps let you
Haven't found this in my search on Stackoverflow - I know I've seen a
Haven't seen this before so hopefully someone has a easy solve, I have a
Haven't seen many Geneva related questions yet, I have posted this question in the
Haven't slept in awhile, so I'm probably missing something simple. Basically, I am taking
Haven't found in docs. Does java ResultSet supports query arguments,like jdbcTemplate? For example, something

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.