Extract all possible subsequences from vector in R [duplicate] - r

This question already has answers here:
Unordered combinations of all lengths
(3 answers)
Closed 2 years ago.
I would like to extract all possible subsequences from a vector of length n. For example if I have c("a","b","c") I would like to find c("a","b","c","ab","ac","bc","abc",""). I can solve this manually with vector of short lengths but the problem becomes intractable with longer lengths. Any idea how I can do this using some iteration?

Using combn.
x <- c("a","b","c")
unlist(sapply(1:length(x), function(i) combn(x, i, simplify=F)), recursive=F)
# [[1]]
# [1] "a"
#
# [[2]]
# [1] "b"
#
# [[3]]
# [1] "c"
#
# [[4]]
# [1] "a" "b"
#
# [[5]]
# [1] "a" "c"
#
# [[6]]
# [1] "b" "c"
#
# [[7]]
# [1] "a" "b" "c"
Or
unlist(sapply(1:length(x), function(i) combn(x, i, function(j) Reduce(paste0, j))))
# [1] "a" "b" "c" "ab" "ac" "bc" "abc"

Related

Split a vector into non-overlapping sub-list with increasing length

Let's say I have this vector:
letters[1:7]
[1] "a" "b" "c" "d" "e" "f" "g"
I would like to split it into a non-overlapping list with increasing length of 1, and keep what is left behind (e.g. sub-list 4 should have 4 elements, but there's only one left, and I'd like to keep that one), like the following:
[[1]]
[1] "a"
[[2]]
[1] "b" "c"
[[3]]
[1] "d" "e" "f"
[[4]]
[1] "g"
Please do let me know any direction to solve this, thank you!
Example vector:
x <- letters[1:7]
Solution:
n <- ceiling(0.5 * sqrt(1 + 8 * length(x)) - 0.5)
split(x, rep(1:n, 1:n)[1:length(x)])
#$`1`
#[1] "a"
#
#$`2`
#[1] "b" "c"
#
#$`3`
#[1] "d" "e" "f"
#
#$`4`
#[1] "g"
Something quick'n dirty
splitter = function(x) {
n = length(x)
i = 1
while ( i * (i + 1L) / 2L < (n-i) ) i = i + 1
out = rep(i+1, n)
out[1:(i * (i + 1L) / 2L)] = rep(1:i, 1:i)
unname(split(x, out))
}
splitter(x)
[[1]]
[1] "a"
[[2]]
[1] "b" "c"
[[3]]
[1] "d" "e" "f"
[[4]]
[1] "g"
x <- letters[1:7]
splt <- rep(seq(length(x)), seq(length(x)))[seq(length(x))]
split(x, splt)
#> $`1`
#> [1] "a"
#>
#> $`2`
#> [1] "b" "c"
#>
#> $`3`
#> [1] "d" "e" "f"
#>
#> $`4`
#> [1] "g"
Created on 2022-08-04 by the reprex package (v2.0.1)

join two lists in R offset by a lag of 1 [duplicate]

This question already has answers here:
How to merge 2 vectors alternating indexes?
(6 answers)
Closed 2 years ago.
Suppose we have two lists:
l1 <- list("a", "b", "c")
l2 <- list("d", "e", "f")
How can we combine them into a single list to obtain this output?
[[1]]
[1] "a"
[[2]]
[1] "d"
[[3]]
[1] "b"
[[4]]
[1] "e"
[[5]]
[1] "c"
[[6]]
[1] "f"
In practice, I need a general solution that scales to lists of any size, and lists with any object (e.g., dataframe, other lists, etc). In other words, pretend that the letters in the list above are actually data.frames
You can do :
as.list(c(rbind(l1, l2)))
#[[1]]
#[1] "a"
#[[2]]
#[1] "d"
#[[3]]
#[1] "b"
#[[4]]
#[1] "e"
#[[5]]
#[1] "c"
#[[6]]
#[1] "f"

How can we use r or excel or minitab to list all the permutations? [duplicate]

This question already has answers here:
How to generate permutations or combinations of object in R?
(3 answers)
Closed 2 years ago.
How can I use R to list all the permutations of 4 numbers??enter image description here
> library(combinat)
> permn(letters[1:3])
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "a" "c" "b"
[[3]]
[1] "c" "a" "b"
[[4]]
[1] "c" "b" "a"
[[5]]
[1] "b" "c" "a"
[[6]]
[1] "b" "a" "c"

appending to multiple elements of a list in R

Suppose you have a list foo containing some elements.
foo <- list()
foo[1:3] <- "a"
foo
# [[1]]
# [1] "a"
# [[2]]
# [1] "a"
# [[3]]
# [1] "a"
I would like to efficiently grow the list by both appending to existing elements and adding additional elements. For example adding "b" to elements 2:5, as simply as possible, preferably using foo[2:5]<-.
Desired output
# [[1]]
# [1] "a"
# [[2]]
# [1] "a" "b"
# [[3]]
# [1] "a" "b"
# [[4]]
# [1] "b"
# [[5]]
# [1] "b"
Oh this indeed works:
foo[2:5] <- lapply(foo[2:5], c, "b")
The c is the concatenation function.

Using rbind()/cbind() to append single row data in R

I have 6 numeric lists each containing different number of values i.e [1:350] , [1:450] .... . I am trying to append all of these lists into a singular list i.e [1:1050] using rbind(), but the output I get is dataframe of [1:350, 1:6].
Can someone please help me with this.
To concatenate multiple lists, you can use c()
x <- list(1, 2:5)
y <- list("A", "B")
z <- list(letters[1:5])
c(x, y, z)
# [[1]]
# [1] 1
#
# [[2]]
# [1] 2 3 4 5
#
# [[3]]
# [1] "A"
#
# [[4]]
# [1] "B"
#
# [[5]]
# [1] "a" "b" "c" "d" "e"

Resources