Reputation: 39605
I dear I have a problem, I have two dataframes like these:
z=data.frame(x1=c(1,2,5,4,9,1,4,2,9,21),x2=c(2,2,2,4,8,9,1,9,1,1),x3=c("a","b","b","a","a","b","b","b","a","a"))
z1=data.frame(y=c("a","b"),x=c("protein","cell"))
I have been trying to match z with z1 considering that levels in y of z1 are the same that levels in x3 of z and I want a new column that shows variable z of z1 in all the data frame z. I would like some like this; I used match but I don't get that result.
x1 x2 x3 N
1 1 2 a protein
2 2 2 b cell
3 5 2 b cell
4 4 4 a protein
5 9 8 a protein
6 1 9 b cell
7 4 1 b cell
8 2 9 b cell
9 9 1 a protein
10 21 1 a protein
Upvotes: 3
Views: 602
Reputation: 109874
This approach use the package qdap
:
library(qdap)
z$N <- z$x3 %l% z1
Yields:
> z
x1 x2 x3 N
1 1 2 a protein
2 2 2 b cell
3 5 2 b cell
4 4 4 a protein
5 9 8 a protein
6 1 9 b cell
7 4 1 b cell
8 2 9 b cell
9 9 1 a protein
10 21 1 a protein
Upvotes: 0
Reputation: 115390
You are looking for merge
You can set by.y
and by.x
appropriately, as they do not have the same name in both columns
merge(z,z1,by.y='y', by.x = 'x3')
x3 x1 x2 x
1 a 1 2 protein
2 a 21 1 protein
3 a 4 4 protein
4 a 9 8 protein
5 a 9 1 protein
6 b 5 2 cell
7 b 2 2 cell
8 b 4 1 cell
9 b 2 9 cell
10 b 1 9 cell
To use match
something like
z$x <- z1[match(z$x3,z1$y),'x']
z
x1 x2 x3 x
1 1 2 a protein
2 2 2 b cell
3 5 2 b cell
4 4 4 a protein
5 9 8 a protein
6 1 9 b cell
7 4 1 b cell
8 2 9 b cell
9 9 1 a protein
10 21 1 a protein
Upvotes: 3