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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:17:46+00:00 2026-06-16T22:17:46+00:00

Possible Duplicate: Processing the list of data.frames with apply family of functions I have

  • 0

Possible Duplicate:
Processing the list of data.frames with “apply” family of functions

I have a dataframe with six numeric variables V1, V2, V3 and V1.lag, V2.lag, V3.lag.

NOTE: My real dataset has much more variables but I use 3 for ilustration only!

I would like to be able to automatically (without hardcoding anything) run through all V variables (not lag variables) and create V1.over.V1.lag variables by dividing each V variable with coresponding lag variable.

df<-data.frame(matrix(rnorm(216),72,6));
colnames(df) <- c("v1.raw", "v2.raw", "v3.raw", "v1.lag", "v2.lag", "v3.lag");

Thanks in advance

**EDIT: I figured how to identify “raw” columns and “lag” columns **

raws <- sapply( names(df), function(x){ unlist(strsplit(x, "[.]"))[2] == "raw" } ); ## which are raw factors

lags <- sapply( names(df), function(x){ unlist(strsplit(x, "[.]"))[2] == "lag" } ); ## which are lagged factors

but I still can’t figure how to divide all raw factors with their lag counterparts

which(raws);

will give me indices, but how do I combine them with lags into new factor?

df[which(raws)] / df[which(lags)]

doesn’t work

  • 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-16T22:17:47+00:00Added an answer on June 16, 2026 at 10:17 pm

    Assuming you have only v.raw and v.lag columns in you data.frame, this should work

      mm <- colnames(df) <- c("v1.raw", "v2.raw", "v3.raw", "v1.lag", "v2.lag", "v3.lag")
      df[,gregexpr('.raw',mm) > 0] /df[,gregexpr('.*lag',mm) > 0]
    

    Edit some explanations to the solution :

    gregexpr('.raw',mm) > 0
    [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE  
    
    head(df[,gregexpr('.raw',mm) > 0],1)
         v1.raw     v2.raw    v3.raw
    1 0.7719037 -0.2078197 -1.223753
    
    regexpr('.lag',mm) > 0
    [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE
    
    head(df[,gregexpr('.lag',mm) > 0],1)
         v1.lag     v2.lag    v3.lag
    1 0.7719037 -0.2078197 -1.223753
    

    Than we use the vectorize / to do division, in one operation.

    Here an example :

    df <- matrix(rep(c(1,2,3,4,5,6),each = 5),ncol=6)
    colnames(df) <- c("v1.raw", "v2.raw", "v3.raw", "v1.lag", "v2.lag", "v3.lag")
        v1.raw v2.raw v3.raw v1.lag v2.lag v3.lag
    [1,]      1      2      3      4      5      6
    [2,]      1      2      3      4      5      6
    [3,]      1      2      3      4      5      6
    [4,]      1      2      3      4      5      6
    [5,]      1      2      3      4      5      6
    
    
    mm <- colnames(df)
    df[,which(gregexpr('.raw',mm) > 0)] /df[,which(gregexpr('.lag',mm) > 0)]
    
       v1.raw v2.raw v3.raw      #as expected 1/4 2/5 3/6
    [1,]   0.25    0.4    0.5 
    [2,]   0.25    0.4    0.5
    [3,]   0.25    0.4    0.5
    [4,]   0.25    0.4    0.5
    [5,]   0.25    0.4    0.5
    

    Edit2 prevent Nan with zero

    df <- matrix(rep(c(1,2,3,4,5,6),each = 5),ncol=6)
    colnames(df) <- c("v1.raw", "v2.raw", "v3.raw", "v1.lag", "v2.lag", "v3.lag")
    df[1,4] <- 0              ## I introduce a 0 here
    mm <- colnames(df)
    ## I use ifelse , because it is vectorize also !
    ## If you find a 0 , don't compute , and retuen me the original value 
    ## You can do other things here 
    ifelse(df[,which(gregexpr('.lag',mm) > 0)] != 0 ,
           df[,which(gregexpr('.raw',mm) > 0)] /df[,which(gregexpr('.lag',mm) > 0)],
           df[,which(gregexpr('.raw',mm) > 0)])  
    
        v1.lag v2.lag v3.lag    ## for some reasons ifelse choose other columns names!(lag not raw)
    [1,]   1.00    0.4    0.5
    [2,]   0.25    0.4    0.5
    [3,]   0.25    0.4    0.5
    [4,]   0.25    0.4    0.5
    [5,]   0.25    0.4    0.5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: List of global user defined functions in JavaScript? I have a java
Possible Duplicate: When processing CSV data, how do I ignore the first line of
Possible Duplicate: Problem with processing individual strings stored in an array of pointers to
Possible Duplicate: Java : Is there a good natural language processing library Can anybody
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to Get Row values in Context menu using cursor? I have
Possible Duplicate: Image Processing in iPhone In my iPhone app, I want to include
Possible Duplicate: How can I subtract a day from a python date? I have
Possible Duplicate: stop php processing file Today while playing around with PHP a noticed
Possible Duplicate: How to get pixel data from a UIImage (Cocoa Touch) or CGImage

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.