file handling in R
2
0
Entering edit mode
4.3 years ago

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 • 894 views
ADD COMMENT
1
Entering edit mode
4.3 years ago
Ram 44k

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.

ADD COMMENT
1
Entering edit mode
4.3 years ago
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
ADD COMMENT
1
Entering edit mode

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)
ADD REPLY
0
Entering edit mode

Appreciate the tips! Edited my original post.

ADD REPLY
1
Entering edit mode

No problem, just spreading the R love!

ADD REPLY
0
Entering edit mode

Thanks all for your answer. I will try it

ADD REPLY

Login before adding your answer.

Traffic: 1743 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6