Dear R-ers,
I feel I am close, but can't get it quite right.
Thanks a lot for your help!
Dimitri
# I have 2 data frames:
x-data.frame(a=c(aa,aa,ab,ab,ba,ba,bb,bb),b=c(1:2,1:2,1:2,1:2),d=c(10,20,30,40,50,60,70,80))
Hi Dimitri,
Try creating a key for x and y and then merging the result by that
variable:
x$key - with(x, paste(a, b, sep = /))
y$key - with(y, paste(a2, b, sep = /))
merge(x, y, by = 'key')[, c(2:4, 8:9)]
HTH,
Jorge.-
On Wed, Jul 11, 2012 at 6:28 PM, Dimitri Liakhovitski wrote:
Dear R-ers,
Jorge, thank you!
that seems to be working, but unfortunately in real life I have
thousands of variables (except for a, a2, a3 and b) so that manually
selecting columns (as in c(2:4, 8:9)) would be too difficult...
Dimitri
On Wed, Jul 11, 2012 at 6:36 PM, Jorge I Velez jorgeivanve...@gmail.com
This should do the trick :
colnames(y)[1:2]=c(a,a)
y2=rbind(y[,-1], y[,-2]) #duplicating the y matrix so the identifiers
are only in 1 column
merged = merge(x,y2)
merged
a b d e1 e2
1 aa 1 10 100 101
2 aa 2 20 200 201
3 ab 1 30 100 101
4 ab 2 40 200 201
5 ba 1 50 300 301
6 ba 2 60 400
Hello,
About many columns like 'e1' and 'e2' I don't know but with the provided
example the following does NOT depend on them, only on 'a', 'b' and 'a2'
and 'a3'.
z - lapply(c(a2, a3), function(cc) merge(x, y, by.x=c(a, b),
by.y=c(cc, b)))
z - lapply(seq_along(z), function(i)
Thanks a lot, everyone for your helpful suggestions!
Eloi, this is very elegant - thank you!
I did not know 2 columns are allowed to have the same names! Always
good to learn something new.
Thanks again!
Dimitri
On Wed, Jul 11, 2012 at 6:58 PM, Mercier Eloi emerc...@chibi.ubc.ca wrote:
This
6 matches
Mail list logo