3

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

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

考虑到 z1 的 y 中的级别与 z 的 x3 中的级别相同,我一直在尝试将 z 与 z1 匹配,并且我想要一个新列来显示所有数据框 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
4

2 回答 2

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
于 2013-03-13T22:08:08.630 回答
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
于 2013-03-14T03:20:30.980 回答