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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:53:39+00:00 2026-05-30T06:53:39+00:00

I have been thinking about this for a while and cannot come up with

  • 0

I have been thinking about this for a while and cannot come up with a solution. I have data in column X that I want to use to create the data in column Z. I want Z to be all 1’s up to the point where there are two 0’s in a row in X, then all zeros after that. Also, in column W I want the final elements to be 1’s when looking at Y from the bottom up, Y contains two 0’s in a row. Hope that makes sense. I have put in column Z and column W how they should end up looking. I am trying to use indexing, but I am having a hard time figuring out how to reference the rows from column X that come after the row where the value for Z will be (because the value in row 1 of Z is based on the values of rows 2 and 3 in X). These should be two separate functions, one to look at the beginning and one to look at the end. They will both be aplplied to each row separately, so column X will produce two columns, Z as below, as well as another column which in this case would be all 0’s. Thanks for any help!

****** I changed the column names from A B C D to X Y Z W to avoid confusion. Sorry, wasn’t thinking of that as I typed it up!

********** I really would like to be able to do this without functions or loops, just using indexing. I think I could figure it out using a function, but since it is a large data set I want it to be as quick as possible.

code    X   Y   Z   W
A   1   0   1   0
A   1   0   1   0
A   0   0   1   0
A   1   0   1   0
A   1   0   1   0
A   1   0   1   0
A   1   0   1   0
A   0   0   1   0
A   1   0   1   0
A   0   0   0   0
A   0   0   0   0
A   1   0   0   0
A   0   0   0   0
A   0   0   0   0
A   0   0   0   0
A   0   0   0   0
A   0   0   0   0
A   0   0   0   0
A   0   0   0   0
A   0   0   0   0
A   0   0   0   0
B   0   0   0   0
B   0   0   0   0
B   0   0   0   0
B   0   0   0   0
B   1   1   0   0
B   0   0   0   0
B   1   0   0   0
B   0   0   0   0
B   1   0   0   0
B   0   0   0   0
B   0   0   0   0
B   1   0   0   0
B   0   1   0   0
B   0   0   0   0
B   0   0   0   0
B   0   1   0   1
B   0   1   0   1
B   0   1   0   1
B   0   0   0   1
B   0   1   0   1
B   0   1   0   1

The following function used with aggregate should give the results I am looking for. Thanks to Tyler for beginning the function. I still feel there should be a simpler way to do this, but for now this should do. Thanks to everyone for your input!

I think I got it figured out, based on Tyler’s code, just with a few changes. I will just apply this function using aggregate and it should all work out. Thanks for all the input!

pat.finder <- function(var, value=0, fill1=1, fill2=0, rev=FALSE, seq=2){

 if(var[1]==0 & rev==FALSE){

 j<- rep(0,length(var))} else if(var[length(var)]==0 & rev == TRUE){

 j<- rep(0,length(var))} else{

 x <- if(rev) rle(rev(var)) else rle(var)
 n <- which(x[[1]]>(seq-1) & x[[2]]==value)[1]-1
 i <- sum(x[[1]][1:n])
 j <- if(rev){
            rev(c(rep(fill1, i), rep(fill2, length(var)-i)))
       } else {
            c(rep(fill1, i), rep(fill2, length(var)-i))
       }
}

 return(j)
} 
  • 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-30T06:53:40+00:00Added an answer on May 30, 2026 at 6:53 am

    There’s probably a faster way but this is what I came up with:

    dat <- read.table(text="code    A   B   C   D #read in your data
    A   1   0   1   0
    A   1   0   1   0
    A   0   0   1   0
    A   1   0   1   0
    A   1   0   1   0
    A   1   0   1   0
    A   1   0   1   0
    A   0   0   1   0
    A   1   0   1   0
    A   0   0   0   0
    A   0   0   0   0
    A   1   0   0   0
    A   0   0   0   0
    A   0   0   0   0
    A   0   0   0   0
    A   0   0   0   0
    A   0   0   0   0
    A   0   0   0   0
    A   0   0   0   0
    A   0   0   0   0
    A   0   0   0   0
    B   0   0   0   0
    B   0   0   0   0
    B   0   0   0   0
    B   0   0   0   0
    B   1   1   0   0
    B   0   0   0   0
    B   1   0   0   0
    B   0   0   0   0
    B   1   0   0   0
    B   0   0   0   0
    B   0   0   0   0
    B   1   0   0   0
    B   0   1   0   0
    B   0   0   0   0
    B   0   0   0   0
    B   0   1   0   1
    B   0   1   0   1
    B   0   1   0   1
    B   0   0   0   1
    B   0   1   0   1
    B   0   1   0   1", header=T)
    

    Now the code:

    A.rle <- rle(dat$A)
    n <- which(A.rle[[1]]>1 & A.rle[[2]]==0)[1]-1
    i <- sum(A.rle[[1]][1:n])
    dat$C <- c(rep(1, i), rep(0, nrow(dat)-i))
    
    B.rle <- rle(rev(dat$B))
    n2 <- which(B.rle[[1]]>1 & B.rle[[2]]==0)[1]-1
    i2 <- sum(B.rle[[1]][1:n2])
    dat$D <- rev(c(rep(1, i2), rep(0, nrow(dat)-i2)))
    

    EDIT: I don’t fully understand what you want I think so I’ve tried to create a function that is versatile to your needs. Use rev=TRUE to look at the end:

    pat.finder <- function(var, value=0, fill1=1, fill2=0, rev=FALSE, seq=2){
        x <- if(rev) rle(rev(var)) else rle(var)
        n <- which(x[[1]]>(seq-1) & x[[2]]==value)[1]-1
        i <- sum(x[[1]][1:n])
        j <- if(rev){
                   rev(c(rep(fill1, i), rep(fill2, length(var)-i)))
              } else {
                   c(rep(fill1, i), rep(fill2, length(var)-i))
              }
        return(j)
    }
    
    #TRY IT OUT
    pat.finder(dat$B, rev=TRUE)
    
    transform(dat, C=pat.finder(A), D = pat.finder(B, rev=TRUE)) #what I think you want
    
    transform(dat, C=pat.finder(A, fill1='foo', fill2='bar'), 
        D = pat.finder(A, rev=TRUE))
    
    transform(dat, C=pat.finder(A, value=1), D = pat.finder(B, rev=TRUE))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been thinking about this problem for a while and have yet to come
Well I have been thinking about this for a while, ever since I was
I have been thinking about the optimal way to create an XML file using
Ok, have a bunch of questions that I have been thinking about the past
I've been thinking about the number of projects we have in-house that are still
I have been thinking about this issue and I can't figure it out. Perhaps
I've been thinking about this for a while and its got to a point
Here's a little problem I've been thinking about for a while now that I
I guess the solution for this is quite simple, but I've been thinking about
I have a dilemna that needs I've been thinking a while but still haven't

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.