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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:14:24+00:00 2026-05-13T19:14:24+00:00

Given data s<-c(1,0,0,0,1,0,0,0,0,0,1,1,1,0,0) I can count 1s and 0s with table or ftable ftable(s,row.vars

  • 0

Given data

s<-c(1,0,0,0,1,0,0,0,0,0,1,1,1,0,0)

I can count 1s and 0s with table or ftable

ftable(s,row.vars =1:1)

and the totals of 11s,01s,10s,00s occurred in s with

table(s[-length(s)],s[-1]).

What would be the clever way to count occurrences of 111s, 011s, …, 100s, 000s? Ideally, I want a table of counts x like

   0 1
11 x x
01 x x
10 x x
00 x x

Is there a general way to compute the total occurrences for all possible sub-sequences of length k=1,2,3,4, … occurred in data?

  • 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-13T19:14:24+00:00Added an answer on May 13, 2026 at 7:14 pm

    Well, it seems like you would first need to generate n-tuples from your vector. The following function should accomplish that:

    makeTuples <- function( x, n ){
    
      # Very inefficient way to loop... but what the heck
      tuples <- list()
    
      for( i in 1:n ){
    
        tuples[[i]] <- x[i:(length(x)-n+i)]
    
      }
    
      return(tuples)
    
    }
    

    Then you could feed the results of makeTuples() to table() using do.call():

    do.call( table, makeTuples(s,3) )
    
    , ,  = 0
    
    
        0 1
      0 4 1
      1 3 1
    
    , ,  = 1
    
    
        0 1
      0 2 1
      1 0 1
    

    This works because the makeTuples() function returns the tuples as a list of lists. The output isn’t quite as nice as you wanted, but you could write a function to reformat, say:

    , ,  = 0
    
    
        0 1
      0 4 1
      1 3 1
    

    To:

         0 1
      00 4 1
      01 3 1
    

    It would require looping over the outer n-2 dimensions of the n-dimensional array returned by table, creating row names and concatenating things together.

    Update

    So, I was just sitting in a Stochastic processes class when I figured out a more or less straight-forward way to produce the output you want without trying to unwind the output of table(). First you will need a function that generates all possible permutations of n selections from your population. The generation of permutations can be done with expand.grid(), but it needs a little sugar-coating:

    permute <- function( population, n ){
    
      permutations <- do.call( expand.grid, rep( list(population), n ) )
    
      permutations <- apply( permutations, 1, paste, collapse = '' )
    
      return( permutations )
    
    }
    

    The basic idea is to iterate over the list of permutations and count the number of tuples that match the given permutation. Since you want the results split out into a table, we should select a permutation of n-1 elements from the population and let the last position form the columns of the table. Here’s a function that takes a permutation of size n-1, a list of tuples, and the population the tuples were drawn from and produces a named vector of match counts:

    countFrequency <- function(permutation,tuples,population){
    
      permutations <- paste( permutation, population, sep = '' )
    
      # Inner lapply applies the equality operator `==` to each
      # permutation and returns a list of TRUE/FALSE vectors.
      # Outer lapply sums the number of TRUE values in each vector. 
      frequencies <- lapply(lapply(permutations,`==`,tuples),sum)
    
      names( frequencies ) <- as.character( population )
    
      return( unlist(frequencies) )
    
    }
    

    Finally, all three functions can be combined into a bigger function that takes a vector, splits it into n-tuples and returns a frequency table. The final aggregation operation is done using ldply() from Hadley Wickham’s plyr package as it does a nice job of preserving information such as which permutation corresponds to which row of output matches:

    permutationFrequency <- function( vector, n, population = unique( vector ) ){
    
      # Split the vector into tuples.
      tuples <- makeTuples( vector, n )
    
      # Coerce and compact the tuples to a vector of strings.
      tuples <- do.call(cbind,tuples)
      tuples <- apply( tuples, 1, paste, collapse = '' )
    
      # Generate permutations of n-1 elements from the population.
      # Turn into a named list for ldply() to work it's magic.
      permutations <- permute( population, n-1 )
      names( permutations ) <- permutations
    
      frequencies <- ldply( permutations, countFrequency,
        tuples = tuples, population = population )
    
      return( frequencies )
    
    }
    

    And there you go:

    require( plyr )
    permutationFrequency( s, 2 )
      .id 1 0
    1   1 2 3
    2   0 2 7
    
    permutationFrequency( s, 3 )
      .id 1 0
    1  11 1 1
    2  01 1 1
    3  10 0 3
    4  00 2 4
    
    permutationFrequency( s, 4 )
      .id 1 0
    1 111 0 1
    2 011 1 0
    3 101 0 0
    4 001 1 1
    5 110 0 1
    6 010 0 1
    7 100 0 2
    8 000 2 2
    
    permutationFrequency( sample( -1:1, 10, replace = T ), 2 )
      .id 1 -1 0
    1   1 1  2 0
    2  -1 0  1 2
    3   0 1  0 2
    

    Apologies to my stochastic processes teacher, but functional programming problems in R were just more interesting than the Gambler’s Ruin today…

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

Sidebar

Related Questions

Given an arbitrarily nested data structure, how can I create a new data structure
How can you sort the following word by the given rule? Example data is
Given that I have a table contains user data, like this: userID calltime result
Given the following data, I would like to count the number of items in
Given a table of input data I would like to have a set o
Can you give an example where the Queue data structure can be specially helpful
I have an IP camera which can give me media-data by RTSP. I develop
Can anyone give me some idea about uploading data from excel to access database
I am trying to sample a data frame from a given data frame such
Another stupid question here regarding a SQL scenario. Given data such as: FieldKey DocumentKey

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.