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

  • Home
  • SEARCH
  • 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 7368057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:36:59+00:00 2026-05-29T03:36:59+00:00

I have a dataframe called prices_df to which I add an extra column and

  • 0

I have a dataframe called prices_df to which I add an extra column and conditionally fill, based on the contents of another column in the same df, with a 1

prices_df$itd_1 <- c(NA)
prices_df$itd_1 <- with( prices_df , ifelse(V2.1==1 , 1, NA) )

which gives a 1 in the last column thus

2005-11-16,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,1
2005-11-17,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA
2005-11-18,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA

However, what I would also like to do to is offset this conditional fill such that the output will be, for example, 1 in the column two rows later than the reference column, or perhaps x rows earlier or later, e.g.

2005-11-16,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA
2005-11-17,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA
2005-11-18,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,1

How can I adjust the given code to achieve this offset conditional filling?

  • 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-29T03:37:00+00:00Added an answer on May 29, 2026 at 3:37 am

    Best way is to:

    1. Create a time series object (I prefer zoo)
    2. Create your extra column with a lag (or offset as you call it)
    3. Merge everything together

      prices_df <- read.table(text="2005-11-16,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA
      2005-11-17,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA
      2005-11-18,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA", header=F, sep=",")
      
      library(zoo)
      prices_df.zoo <-zoo(prices_df[,-1],as.Date(prices_df[,1]))
      itd_1 <- lag(with( prices_df.zoo , ifelse(V6==1 , 1, NA) ),-2) #lag = -2
      prices_df.zoo <-merge(prices_df.zoo, itd_1)
      
      > prices_df.zoo
                 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 itd_1
      2005-11-16 NA NA NA NA  1 NA NA NA  NA  NA  NA  NA    NA
      2005-11-17 NA NA NA NA NA NA NA NA  NA  NA  NA  NA    NA
      2005-11-18 NA NA NA NA NA NA NA NA  NA  NA  NA  NA     1
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame called series_to_plot.df which I created by combining a number of
I have a dataframe with a column of integers that I would like to
I have a dataframe called data where I would like to rescale the values
I have a data.frame , called so_data. Columns 13:23 are list s, which hold
I have a file, called genes.txt , which I'd like to become a data.frame.
If I have the following dataframe called result > result Name CV LCB UCB
I have a dataframe. first column contains my the predictive score (range from 0
I have a dataframe with columns labeled A,B & C. I want to add
I have a dataframe called ants detailing multiple entries per site, looks like this:
In R I am trying to delete rows within a dataframe (ants) which have

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.