cbind subsets into one column in r

666 views Asked by At

I have have created subsets of a dataframe, which I used for calculations. I am now left with numberous subsets which I want to combine into one column. The subsets look like this:

> E
$`1`
 [1] "AAAaaa" "TTTaaa" "CCCaaa" "GGGaaa" "AAAttt" "TTTttt" "CCCttt" "GGGttt"
 [9] "AAAccc" "TTTccc" "CCCccc" "GGGccc" "AAAggg" "TTTggg" "CCCggg" "GGGggg"

$`2`
 [1] "ATAata" "TATata" "CGCata" "GCGata" "BBBata" "ATAtat" "TATtat" "CGCtat"
 [9] "GCGtat" "BBBtat" "ATAcgc" "TATcgc" "CGCcgc" "GCGcgc" "BBBcgc" "ATAgcg"
[17] "TATgcg" "CGCgcg" "GCGgcg" "BBBgcg" "ATAbbb" "TATbbb" "CGCbbb" "GCGbbb"
[25] "BBBbbb"

I have tried:

A=vector()
cbind(A,ExonJunction,deparse.level = 1)
A

But that leaves me with

  E
1 Character,16
2 Character,25

I want the list of characters in one column. How do I do this?

3

There are 3 answers

0
akrun On BEST ANSWER

You can unlist the list and create a single column dataframe with data.frame

dat <- data.frame(Col1=unlist(E, use.names=FALSE), stringsAsFactors=FALSE)

data

E <-  structure(list(`1` = c("AAAaaa", "TTTaaa", "CCCaaa", "GGGaaa", 
"AAAttt", "TTTttt", "CCCttt", "GGGttt", "AAAccc", "TTTccc", "CCCccc", 
"GGGccc", "AAAggg", "TTTggg", "CCCggg", "GGGggg"), `2` = c("ATAata", 
"TATata", "CGCata", "GCGata", "BBBata", "ATAtat", "TATtat", "CGCtat", 
"GCGtat", "BBBtat", "ATAcgc", "TATcgc", "CGCcgc", "GCGcgc", "BBBcgc", 
"ATAgcg", "TATgcg", "CGCgcg", "GCGgcg", "BBBgcg", "ATAbbb", "TATbbb", 
"CGCbbb", "GCGbbb", "BBBbbb")), .Names = c("1", "2"))
7
David Arenburg On

Could also try the recursive argument in c function, something like

c(E, recursive = TRUE, use.names = FALSE)
#  [1] "AAAaaa" "TTTaaa" "CCCaaa" "GGGaaa" "AAAttt" "TTTttt" "CCCttt" "GGGttt" "AAAccc" "TTTccc" "CCCccc" "GGGccc" "AAAggg" "TTTggg" "CCCggg" "GGGggg" "ATAata"
# [18] "TATata" "CGCata" "GCGata" "BBBata" "ATAtat" "TATtat" "CGCtat" "GCGtat" "BBBtat" "ATAcgc" "TATcgc" "CGCcgc" "GCGcgc" "BBBcgc" "ATAgcg" "TATgcg" "CGCgcg"
# [35] "GCGgcg" "BBBgcg" "ATAbbb" "TATbbb" "CGCbbb" "GCGbbb" "BBBbbb"

Or if you want it as a column within a data frame, could try

df <- data.frame(Res = c(E, recursive = TRUE))
0
A5C1D2H2I1M1N2O1R2T1 On

You can also use stack, like this (provided you are dealing with a named list, like you are):

stack(E)

A nice feature is that the names become the "ind" column, so the process is easily reversible.

head(stack(E))
#   values ind
# 1 AAAaaa   1
# 2 TTTaaa   1
# 3 CCCaaa   1
# 4 GGGaaa   1
# 5 AAAttt   1
# 6 TTTttt   1
tail(stack(E))
#    values ind
# 36 BBBgcg   2
# 37 ATAbbb   2
# 38 TATbbb   2
# 39 CGCbbb   2
# 40 GCGbbb   2
# 41 BBBbbb   2