0

Is there an easy way to alternately merge two columns into one list/dataframe column in R? Essentially I need to do a large series of manipulation of genetic data, sorting etc... and eventually I am left with an identifier and a sequence. What is the easiest way to create a single list that has each identifier above its sequence in a single list?

I have data: df <- data.frame(genenames = c("gene1", "gene2", "gene3"), seqnes = c("gattaca", "gatgatcca", "catgatcat"))

I would like to get to: data.frame(c("gene1", "gattaca", "gene2","gatgatcca", "gene3", "catgatcat"))

using either unite(data, "identifier", "sequence", sep = " ") will result in all data fuse within a single column as well as stack(......)

Is there an easy or elegant way to do this save starting to look into writing a small loop to iteratively write out the new list?

slava-kohut
  • 4,203
  • 1
  • 7
  • 24

4 Answers4

2

Is this what you want?

> data.frame("wut"=unname(unlist(data.frame(t(df)))))
        wut
1     gene1
2   gattaca
3     gene2
4 gatgatcca
5     gene3
6 catgatcat
user2974951
  • 9,535
  • 1
  • 17
  • 24
2

Elegant one-liner. No column specs and loops are needed.

df <- data.frame(new_col = c(t(df)))
slava-kohut
  • 4,203
  • 1
  • 7
  • 24
1

Here is one potential solution with gather from tidyrand some dplyr

library(dplyr)
library(tidyr)

df %>%
  mutate(obs = 1:nrow(df)) %>%
  gather(identifier, sequence, -obs) %>%
  arrange(obs, desc(identifier))
Bjørn Kallerud
  • 979
  • 8
  • 23
0

Making a list:

lapply(apply(df, 1, identity), unlist)