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

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

CC BY-SA 4.0
2019-08-17T11:32:30.103Z
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
kcm
CC BY-SA 4.0
2019-08-17T11:39:17.667Z
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.
haci
CC BY-SA 4.0
2019-08-17T11:47:08.983Z
"If you prefer to have separate (merged) files" yes this is what i would like to .I will give it a try
kcm
CC BY-SA 4.0
2019-08-17T12:11:18.887Z
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"
kcm
CC BY-SA 4.0
2019-08-17T12:39:35.000Z
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.
haci
CC BY-SA 4.0
2019-08-17T14:01:36.643Z
okay but if i do this A <- inner_join(ANNOTATION_FILE,module_darkgreen)
Joining, by = "Gene" i get a file mapped .Not sure what is the issue
kcm
CC BY-SA 4.0
2019-08-17T14:14:20.263Z
im using the same code as given here
kcm
CC BY-SA 4.0
2019-08-17T11:27:44.303Z
2019-08-17T13:25:43.963Z
2019-08-17T13:25:43.963Z
haci
<p>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.</p>

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

<p>So what im doing is as such </p>

<pre><code>filelist = list.files(pattern = ".*.txt")
</code></pre>

<p>I get list of files </p>

<p>Then those list of files into list of dataframe</p>

<pre><code>datalist = lapply(filelist, function(x)read.table(x, header=T)) 
</code></pre>

<p>Now i read into the annotation file </p>

<pre><code>ANNOTATION_FILE &lt;- read_delim("/run/media/punit/data1/Dev_bio_info/ANNOTATION_FILE.txt", 
    "\t", escape_double = FALSE, trim_ws = TRUE)
</code></pre>

<p>The normal way i do is </p>

<p>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.</p>

<p>My data frame example </p>

<p>This is how my module files looks like .</p>

<pre><code>dput(head(module_ivory[c(1:10)]))
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))
</code></pre>

<p>My annotation file </p>

<pre><code>dput(head(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))
</code></pre>

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

<p>Any help or suggestion would be really appreciated </p>

CC BY-SA 4.0
2019-08-17T11:16:23.377Z
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).
llrs
<r>
Mapping probe ID to gene name
# Mapping probe ID to gene name

(Asked by: username_0 on 2019-08-17)

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 .
dput(head(module_ivory[c(1:10)]))
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 
dput(head(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

**Tags:** r
> **Comments:**
> **username_1**: 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).  

---

## 1 answers

### Answer by username_2 on 2019-08-17. Score: 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.

> **Comments:**
> **username_0**: 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  
> **username_2**: 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.  
> **username_0**: "If you prefer to have separate (merged) files" yes this is what i would like to .I will give it a try  
> **username_0**: 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"  
> **username_2**: 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.  
> **username_0**: okay but if i do this A <- inner_join(ANNOTATION_FILE,module_darkgreen) Joining, by = "Gene" i get a file mapped .Not sure what is the issue  
> **username_0**: im using the same code as given here  

---

