【问题标题】:How can I get the number of all the possible numerical combinations with a certain length in a vector which contains many different numbers?How can I get the number of all the possible numerical combinations with a certain length in a vector which contains many different numbers?
【发布时间】:2022-12-02 00:20:58
【问题描述】:

I am trying to obtain the number of times that a certain numerical combination is repeated within a vector.

For example, I have this sequence of different numbers and I want to understand how many times is repeated a certain "block of number":

test <- c(1,2,3,4,1,2,3,4,4,4,4,4)

I would like to choose the length of the "block". Let's say for example length=4.

If there's some base function or solutions in R that you know, I would like to get all the possible outcomes that sould be:

2 times I found the combination 1 2 3 4 1 time I found the combination 4 4 4 4

Could you help me to fix this? I'm interested in knowing both the number of times a certain combination has been found, and also what numbers make up that combination.

I also tried to fix it with hints from Generate list of all possible combinations of elements of vector but I'm not able to obtain the results which I expect.

【问题讨论】:

  • Hi, although your explanation stricts much of context, its combinatorial interpretation is still prolific. Do you mean (?): given a sequence S and a word w, I look for how many times this word appears on S.

标签: r combinations permutation calculation


【解决方案1】:

Based on your desired output (and thus not all possible chunks of length n in the vector), a way could be to split vector into chunks of the desired length, convert them to strings, count the occurrences, stack them and paste to get the desired output.

You'll want to decide yourself how it should handle cases where length(test)/n is not an integer. It could throw an error or you might want to round in some way.

n <- 4

results <-
  split(test, cut(seq_along(test), length(test)/n, labels = FALSE)) |> 
  lapply(FUN = (x) paste(x, collapse = " ")) |>
  unlist() |>
  table() |>
  stack()

paste(results$values, "times I found the combination", results$ind)

Output:

[1] "2 times I found the combination 1 2 3 4" "1 times I found the combination 4 4 4 4"

【讨论】:

  • Thanks a lot for the answer @harre. How could I handle cases where I don't know the block length?
【解决方案2】:

Statement: Given sequence S and word w, find natural n respective to quantity of times w appears on S.

Definition: we define a sequence as a concatenation of elements.

Definition: we define a word as a synonym to a sequence.

Requirements: Word cardinality #w is lower than #S

Algorithm:

  1. Place word w at beginning of sequence S;
  2. Initialize counter c to 0;
  3. Verifies if it matches: if the answer is yes, increase c by 1;
  4. Move word w right by 1 element.

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 2021-07-18
    • 2022-12-01
    • 2022-12-02
    • 2022-12-02
    • 2022-12-26
    • 2022-11-20
    • 2022-12-27
    • 2022-12-01
    相关资源
    最近更新 更多