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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:16:57+00:00 2026-05-22T20:16:57+00:00

From a dataframe I get a new array, sliced from a dataframe. I want

  • 0

From a dataframe I get a new array, sliced from a dataframe.
I want to get the amount of times a certain repetition appears on it.

For example

main <- c(A,B,C,A,B,V,A,B,C,D,E)
p <- c(A,B,C)
q <- c(A,B)

someFunction(main,p)
2

someFunction(main,q)
3

I’ve been messing around with rle but it counts every subrepetion also, undersirable.

Is there a quick solution I’m missing?

  • 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-22T20:16:58+00:00Added an answer on May 22, 2026 at 8:16 pm

    You can use one of the regular expression tools in R since this is really a pattern matching exercise, specifically gregexpr for this question. The p and q vectors represent the search pattern and main is where we want to search for those patterns. From the help page for gregexpr:

    gregexpr returns a list of the same length as text each element of which is of 
    the same form as the return value for regexpr, except that the starting positions 
    of every (disjoint) match are given. 
    

    So we can take the length of the first list returned by gregexpr which gives the starting positions of the matches. We’ll first collapse the vectors and then do the searching:

    someFunction <- function(haystack, needle) {
        haystack <- paste(haystack, collapse = "")
        needle <- paste(needle, collapse = "")
        out <- gregexpr(needle, haystack)
        out.length <- length(out[[1]])
        return(out.length)
    }
    
    > someFunction(main, p)
    [1] 2
    > someFunction(main, q)
    [1] 3
    

    Note – you also need to throw “” around your vector main, p, and q vectors unless you have variables A, B, C, et al defined.

    main <- c("A","B","C","A","B","V","A","B","C","D","E")
    p <- c("A","B","C")
    q <- c("A","B")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread.
From what I can gather, there are three categories: Never use GET and use
I want to filter rows from a data.frame based on a logical condition. Let's
Firstly, I'm relatively new to network programming. I want to intercept and delay HTTP
Is there a simple way to convert data in a dataframe from fraction to
I´m trying to clean the factor variables in a dataframe from trailing spaces. However
I would like to get a sum from a column, with and without a
If all tables I want to delete from have the column gamer_id can i
I'm trying to get a time series of returns for holding a certain asset
Every time I get a new data set the first thing I do is

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.