This is a test version of Biostars. For the public version, visit https://www.biostars.org.
file handling in R

i want to extract gene column (2nd column of every file) from multiple text files and merge them to get unique gene list. But in every text file number of rows differs. can someone please tell me how to write R code for this? Thanks in advance

r

2 answers

Your could use lapply with rbind, or just use cut/awk followed by sort -u on the shell. There's no need for R here.

if(!require("tidyverse")) install.packages("tidyverse")
files <- purrr::map_dfr(list.files(pattern = "\\.txt$"), read_tsv)

Then if you only want the Genes column:

files <- files$Genes

It will work either way, but with the map functions in this case you can just put the name of the function at the end. You don't need to call it as an anonymous function. Also, you should escape the period in the file extension, otherwise it would match any character. I usually put an end of line in the regex too for good measure.

files <- purrr::map_dfr(list.files(pattern = "\\.txt$"), read_tsv)

No problem, just spreading the R love!

Log in to answer this question.