Hi everybody I am working with a list of data frames in R that have the same variable names for all. The structure of my list is the next, I only include five elements to make it reproducible but I can have more than 20 elements in the list:
list
$a1
ID G
00001 A
00002 A
00003 B
00004 C
00005 D
00006 A
$a2
ID G
00001 A
00002 A
00003 B
00004 C
00005 D
00006 A
00007 A
$a3
ID G
00001 A
00002 A
00003 B
00004 C
00005 D
00006 A
00007 A
00008 B
$a4
ID G
00001 A
00002 A
00003 B
00004 C
00005 D
00006 A
00007 A
00008 B
00009 C
$a5
ID G
00001 A
00002 A
00003 B
00004 C
00005 D
00006 A
00007 A
00008 B
00009 C
00010 D
Where the names of elements are a1
, a2
, a3
, a4
and a5
. My problem starts when I merge all elements because I can't establish a difference between merged variables. For example I apply to list
next code to merge it:
Merged=Reduce(function(x, y) merge(x, y,all.x=T,by=1),list)
and I got this for Merged
ID G.x G.y G.x G.y G
00001 A A A A A
00002 A A A A A
00003 B B B B B
00004 C C C C C
00005 D D D D D
00006 A A A A A
And this warnings:
Warnings:
1: In merge.data.frame(x, y, all.x = T, by = 1) :
column names ‘G.x’, ‘G.y’ are duplicated in the result
2: In merge.data.frame(x, y, all.x = T, by = 1) :
column names ‘G.x’, ‘G.y’ are duplicated in the result
The merge is fine but I can't difference between merged variables because they have the same names. I would like to difference them for example first g.x
is group from a1
, first g.y
is group from a2
, second g.x
is group from a3
, second g.y
is group from a4
and g
is group from a5
. I want to difference g
considering the element where it comes, and I would like a structure like this:
ID G.1 G.2 G.3 G.4 G.5
00001 A A A A A
00002 A A A A A
00003 B B B B B
00004 C C C C C
00005 D D D D D
00006 A A A A A
Where clearly I can difference from what data frame comes each G
or at least I would like something where I can make this difference. Thanks for your help.