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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:48:32+00:00 2026-06-15T17:48:32+00:00

In R, I have data a vector of integers. run <- sample.int(9, 1000, replace=T)

  • 0

In R, I have data a vector of integers.

run <- sample.int(9, 1000, replace=T)
run[sample.int(1000, 100)] <- NA

If at least one of the following patterns, c(1, x, 1, y) or c(x, 1, y, 1) where x and y are either whole numbers or NA, is present, I would like to print out the start index of each pattern and update a count variable for each instance of a pattern. What is the most efficient way of doing this?

I was thinking of using the rle function and testing for every 4 consecutive values for a length of 1, and then testing whether they conform to one of the patterns. However, I am having problems with NAs with this approach since each NA is treated separately. Perhaps there is a better way to do this.

  • 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-15T17:48:34+00:00Added an answer on June 15, 2026 at 5:48 pm

    Taking your usage of sample.int as implying your vector only contains values from 1:9 and NA, here’s a regular expressions approach:

    run <- c(1, NA, 1, 3, 1, 1, NA, NA, NA, 1)
    run[is.na(run)] <- 0
    pat1 <- "(?=1[0-9]1[0-9])" # using a lookahead assertion around the pattern is a way to allow overlapping matches
    pat1.idxs <- unlist(gregexpr(pat1, paste(run, collapse=''), perl=TRUE))
    pat1.idxs
    # match indexes
    # [1] 1 3
    length(pat1.idxs)
    # counts
    # [1] 2
    

    Then you would do second pattern similarly.

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

Sidebar

Related Questions

I have the following data structure (an atomic vector?) output from daply in plyr
I have a vector of data, which contains integers in the range -20 20.
So, I have a vector that is either full of integers. Lets call this
I have the following member data vector<State<T>*> activeChildren; I want to clean-up these pointers
Say I have a dynamic array like: int* integers = new int[100]; Is there
I have a large nested vector that look like this: import Data.Vector let x
I have some data structures: all_unordered_m is a big vector containing all the strings
I have a problem with my iOS application. I receive binary vector data from
Say I have a vector: (def data [Hello World Test This]) And I want
I'd like a generic method for retrieving the data from a vector. I 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.