I have set of modules from various condition ,with first column as Probe_ID name and I have an annotation file with Probe_ID and Gene_name.

I have to map the Probe_ID to gene name in each file and write it back.

So what im doing is as such

filelist = list.files(pattern = ".*.txt")

I get list of files

Then those list of files into list of dataframe

datalist = lapply(filelist, function(x)read.table(x, header=T)) 

Now i read into the annotation file

ANNOTATION_FILE <- read_delim("/run/media/punit/data1/Dev_bio_info/ANNOTATION_FILE.txt", 
    "\t", escape_double = FALSE, trim_ws = TRUE)

The normal way i do is

I perform inner_join(ANNOTATION_FILE,my_file) for single file but as i have like 22 files in the module .I want to do it in loop and im not sure how to proceed further.

My data frame example

This is how my module files looks like .

structure(list(Gene = c("1556672_a_at", "1557803_at", "1558080_s_at", 
"1566465_at", "213089_at", "213229_at"), GSM1304905 = c(5.977158746759, 
6.83852532860115, 5.93931007942615, 6.11926533584282, 9.92182534751875, 
10.2576150019626), GSM1304906 = c(7.51622774902859, 7.71268478229187, 
7.32360342148457, 6.8928342169826, 9.12807784837542, 10.6767090567628
), GSM1304907 = c(6.58069278884036, 7.86890363253932, 6.9901005950279, 
6.08614452085924, 10.1220627669628, 10.9203125158285), GSM1304908 = c(6.60119000844375, 
6.71735654004629, 7.43132441110608, 5.48084261893207, 9.81689665710992, 
10.7649566455486), GSM1304909 = c(5.95049879217177, 6.132975671606, 
7.0902951305213, 5.59408027736444, 9.08474568290096, 9.84784203563294
), GSM1304910 = c(5.83616005353191, 5.94947517647125, 6.61469427098759, 
4.31285839654272, 9.44446310110043, 9.99021299504538), GSM1304911 = c(5.92373056182016, 
6.14502105445603, 6.2333758614231, 5.46455403294761, 9.55447963883665, 
10.1810888944642), GSM1304912 = c(6.43107367158601, 6.36340779490876, 
7.18374247647159, 5.71508717609646, 9.61355221684561, 10.3266656836978
), GSM1304913 = c(6.4010171640209, 7.31600259413326, 6.16969520557938, 
5.40234538288784, 8.89481478840621, 10.290064262843)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L))

My annotation file

structure(list(Gene = c("1007_s_at", "1053_at", "117_at", "121_at", 
"1255_g_at", "1294_at"), Gene_Symbol = c("DDR1 /// MIR4640", 
"RFC2", "HSPA6", "PAX8", "GUCA1A", "MIR5193 /// UBA7")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L))

I would like to do inner join on each file and write them back into the file .

Any help or suggestion would be really appreciated

  • $\begingroup$ You mention that you tried a loop , could you show us the code you used and why it didn't work? Also I'm surprised you needed inner_join. Haven't the annotation for your microarray been helpful to map your IDs. Also probably it is worth to first add the annotation you want with before splitting into files (you seem to be using WGCNA or some sort of clustering algorithm, so you could add the gene name before splitting them into 22 files). $\endgroup$
    – llrs
    Aug 17, 2019 at 11:16

1 Answer 1


To end up with a single table made by merging all your tables, you can give a try with reduce() of purrr: The first argument .x would be the list of tables to merge (in your case the annotation file plus all the other files) and the .y argument would be your function, i.e. inner_join(). reduce() would merge all the tables within the given list, left to right or right to left. ... would enable you to specify column(s) to use as "anchors" while merging.

If you would prefer to keep your tables separately then lapply() is the answer: lapply(list_of_files, function(x) inner_join(annotation_file, x)). This would give a list of merged tables.

  • $\begingroup$ just a doubt is it going to merge all the tables "would merge all the tables within the given list," as you wrote this .Because i want to keep all the individual files separately $\endgroup$
    – kcm
    Aug 17, 2019 at 11:32
  • 1
    $\begingroup$ reduce() would merge everything and would provide a single table. If you prefer to have separate (merged) files, lapply() would help. I have edited my answer to reflect these. $\endgroup$
    – haci
    Aug 17, 2019 at 11:39
  • $\begingroup$ "If you prefer to have separate (merged) files" yes this is what i would like to .I will give it a try $\endgroup$
    – kcm
    Aug 17, 2019 at 11:47
  • $\begingroup$ I ran to this error "Error: by required, because the data sources have no common variables Call rlang::last_error() to see a backtrace " even though I have a common column name called "Gene" $\endgroup$
    – kcm
    Aug 17, 2019 at 12:11
  • $\begingroup$ Works for me with the limited data you provided in your post (except for getting 0 x 11 tables as there are no shared gene identifiers in your annotations table and your data table, hence inner_join() returning 0 rows). Please edit your original post as to include the code you used. $\endgroup$
    – haci
    Aug 17, 2019 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.