考虑两个变量匹配两个数据帧具有相同的级别

时间:2013-03-13 22:03:49

标签: r

亲爱的,我有一个问题,我有两个这样的数据帧:

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"))

我一直试图将z与z1匹配,考虑到z1的y中的水平与z的x3中的水平相同,并且我想要在所有数据帧z中显示z1的变量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

2 个答案:

答案 0 :(得分:3)

您正在寻找merge

您可以相应地设置by.yby.x,因为它们在两列中都没有相同的名称

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

使用match之类的内容

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

答案 1 :(得分:0)

此方法使用包qdap

library(qdap)
z$N <- z$x3 %l% z1

<强>收率:

> 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