Question: file handling in R
0
gravatar for smrutimayipanda
11 days ago by
smrutimayipanda10 wrote:

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 • 132 views
ADD COMMENTlink modified 10 days ago by bioinformatics2020330 • written 11 days ago by smrutimayipanda10
1
gravatar for RamRS
11 days ago by
RamRS28k
Houston, TX
RamRS28k wrote:

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 COMMENTlink written 11 days ago by RamRS28k
1
gravatar for bioinformatics2020
10 days ago by
bioinformatics2020330 wrote:
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 COMMENTlink modified 10 days ago • written 10 days ago by bioinformatics2020330
1

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 REPLYlink modified 10 days ago • written 10 days ago by rpolicastro720

Appreciate the tips! Edited my original post.

ADD REPLYlink written 10 days ago by bioinformatics2020330
1

No problem, just spreading the R love!

ADD REPLYlink written 10 days ago by rpolicastro720

Thanks all for your answer. I will try it

ADD REPLYlink written 10 days ago by smrutimayipanda10
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1617 users visited in the last hour