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"
Related
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"
I have data with 'from' and 'to' columns:
df = data.frame(from = c('A','A','X','E','B','W','C','Y'),
to = c('B','E','Y','C','A','X','A','W'))
I'd like to identify all sequences of 'from-to', considering two or more rows, which starts and ends on the same value. An easy one would be A-B-A:
# df
# from to
# 1 A B # 1. From A to B
# 2 A E
# 3 X Y
# 4 E C
# 5 B A # 2. From B and back to the starting point A, completing the sequence A-B-A
# 6 W X
# 7 C A
# 8 Y W
Another one:
# df
# from to
# 1 A B
# 2 A E # 1.
# 3 X Y
# 4 E C # 2.
# 5 B A
# 6 W X
# 7 C A # 3. -> Thus: A - E - C - A
# 8 Y W
There is also e.g. X - Y - W - X
How can I find such cycles?
Here is another option:
library(igraph)
g <- graph_from_data_frame(h)
#https://lists.nongnu.org/archive/html/igraph-help/2009-04/msg00125.html
find.cycles <- function(graph, k) {
ring <- graph.ring(k, TRUE)
subgraph_isomorphisms(ring, graph)
}
#find all cycles
N <- length(unique(unlist(h)))
l <- unlist(lapply(1L:N, find.cycles, graph=g), recursive=FALSE)
#extract the vertices in each cycle
Filter(Negate(is.null), lapply(l, function(e) {
if (length(e) > 1L) {
nm <- names(e)
c(nm, nm[1L])
}
}))
output:
[[1]]
[1] "A" "B" "A"
[[2]]
[1] "B" "A" "B"
[[3]]
[1] "A" "E" "C" "A"
[[4]]
[1] "X" "Y" "W" "X"
[[5]]
[1] "E" "C" "A" "E"
[[6]]
[1] "W" "X" "Y" "W"
[[7]]
[1] "C" "A" "E" "C"
[[8]]
[1] "Y" "W" "X" "Y"
Reference:
Re: [igraph] Help - find cycles by Gábor Csárdi
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.
I hope this question is not a duplicate, because I searched it didn't find any answer(If its a dupe, please let me know I shall remove it).
I am trying to print/display the contents of an environment, but I am unable to do it.
library(rlang)
e1 <- env(a = 1:10, b= letters[1:5])
When I use print, It just give me memory address not the contents(names and values) of that environment.
> print(e1)
<environment: 0x00000000211fbae8>
Note: I can see the env. contents in R studio Environments tab, I am using R version: "R version 3.4.2" and rlang: rlang_0.2.0
My question is : What is the right function to print contents of an environment, Sorry the question may be naive, but I am unable to figure out.
Thanks in advance
We can use get with envir parameter to get values out of specific environment
sapply(ls(e1), function(x) get(x, envir = e1))
#$a
# [1] 1 2 3 4 5 6 7 8 9 10
#$b
#[1] "a" "b" "c" "d" "e"
where
ls(e1) # gives
#[1] "a" "b"
We can use mget
mget(ls(e1), envir = e1)
#$a
#[1] 1 2 3 4 5 6 7 8 9 10
#$b
#[1] "a" "b" "c" "d" "e"
An option can be as:
lapply(ls(),function(x)get(x))
which prints content of the global environment.
#Result:
# [[1]]
# [1] 1 2
#
# [[2]]
# [1] 1 4
#
# [[3]]
# [1] 1 1
#
# [[4]]
# function (snlq)
# {
# j <- 1
# for (i in 1:length(snlq)) {
# ind <- index(snlq[[i]])
# if (identical(ind[length(ind)], "2018-05-04") == FALSE) {
# ss[j] <- i
# j <- j + 1
# }
# }
# return(ss)
# }
# <bytecode: 0x000000001fa07290>
#
#... so on
My question is very simple..but I cant manage to work it out...
I have run a variable selection method in R on 2000 genes using 1000 iterations and in each iteration I got a combination of genes. I would like to count the number of times each combination of genes occurs in R.
For example I have
# for iteration 1
genes[1] "a" "b" "c"
# for iteration 2
genes[2] "a" "b"
# for iteration 3
genes[3] "a" "c"
# for iteration 4
genes [4] "a" "b"
and this would give me
"a" "b" "c" 1
"a" "b" 2
"a" "c" 1
I have unlisted the list and got the number each gene comes but I am interested in is the combination. I tried to create a table but I have unequal length for each gene vector. Thanks in advance.
The way I could immediately think of is to paste them and then use table as follows:
genes_p <- sapply(my_genes, paste, collapse=";")
freq <- as.data.frame(table(genes_p))
# Var1 Freq
# 1 a;b 2
# 2 a;b;c 1
# 3 c 1
The above solution assumes that the genes are sorted by names and the same gene id doesn't occur more than once within an element of the list. If you want to account for both, then:
# sort genes before pasting
genes_p <- sapply(my_genes, function(x) paste(sort(x), collapse=";"))
# sort + unique
genes_p <- sapply(my_genes, function(x) paste(sort(unique(x)), collapse=";"))
Edit: Following OP's question in comment, the idea is to get all combinations of 2'ers (so to say), wherever possible and then take the table. First I'll break down the code and write them separate for understanding. Then I'll group them together to get a one-liner.
# you first want all possible combinations of length 2 here
# that is, if vector is:
v <- c("a", "b", "c")
combn(v, 2)
# [,1] [,2] [,3]
# [1,] "a" "a" "b"
# [2,] "b" "c" "c"
This gives all the combinations taken 2 at a time. Now, you can just paste it similarly. combn also allows function argument.
combn(v, 2, function(y) paste(y, collapse=";"))
# [1] "a;b" "a;c" "b;c"
So, for each set of genes in your list, you can do the same by wrapping this around a sapply as follows:
sapply(my_genes, function(x) combn(x, min(length(x), 2), function(y)
paste(y, collapse=";")))
The min(length(x), 2) is required because some of your gene list can be just 1 gene.
# [[1]]
# [1] "a;b" "a;c" "b;c"
# [[2]]
# [1] "a;b"
# [[3]]
# [1] "c"
# [[4]]
# [1] "a;b"
Now, you can unlist this to get a vector and then use table to get frequency:
table(unlist(sapply(l, function(x) combn(x, min(length(x), 2), function(y)
paste(y, collapse=";")))))
# a;b a;c b;c c
# 3 1 1 1
You can wrap this in turn with as.data.frame(.) to get a data.frame:
as.data.frame(table(unlist(sapply(l, function(x) combn(x, min(length(x), 2),
function(y) paste(y, collapse=";"))))))
# Var1 Freq
# 1 a;b 3
# 2 a;c 1
# 3 b;c 1
# 4 c 1