24

复杂的标题,但这是我想要实现的一个简单示例:

d <- data.frame(v1 = c(1,2,3,4,5,6,7,8), 
                v2 = c("A","E","C","B","B","C","A","E"))

m <- data.frame(v3 = c("D","E","A","C","D","B"), 
                v4 = c("d","e","a","c","d","b"))

通过匹配in 中的值,d$v2应将 in 中的值替换为 in 中的值m$v4d$v2m$v3

生成的数据框d应如下所示:

v1    v4
1      a
2      e
3      c
4      b
5      b
6      c
7      a
8      e

我尝试了不同的东西,最接近的是:d$v2 <- m$v4[which(m$v3 %in% d$v2)]

我尝试再次避免任何 for 循环!必须是可能的:-) 不知何故... ;)

4

3 回答 3

20

你可以试试:

merge(d,m, by.x="v2", by.y="v3")
  v2 v1 v4
1  A  1  a
2  A  7  a
3  B  4  b
4  B  5  b
5  C  3  c
6  C  6  c
7  E  2  e
8  E  8  e

编辑

这是另一种保留顺序的方法:

data.frame(v1=d$v1, v4=m[match(d$v2, m$v3), 2])
  v1 v4
1  1  a
2  2  e
3  3  c
4  4  b
5  5  b
6  6  c
7  7  a
8  8  e
于 2012-07-17T20:20:54.240 回答
11

您可以使用标准的左连接。

加载数据:

d <- data.frame(v1 = c(1,2,3,4,5,6,7,8), v2 = c("A","E","C","B","B","C","A","E"), stringsAsFactors=F)
m <- data.frame(v3 = c("D","E","A","C","D","B"), v4 = c("d","e","a","c","d","b"), stringsAsFactors=F)

更改列名,以便我可以按列“v2”加入

colnames(m) <- c("v2", "v4")

左加入并保持data.frame d的顺序

library(dplyr)
left_join(d, m)

输出:

  v1 v2 v4
1  1  A  a
2  2  E  e
3  3  C  c
4  4  B  b
5  5  B  b
6  6  C  c
7  7  A  a
8  8  E  e
于 2016-03-17T15:10:24.050 回答
8

这将为您提供所需的输出:

d$v2 <- m$v4[match(d$v2, m$v3)]

match 函数返回 m 矩阵的 v3 列中d$v2被匹配值的位置。一旦获得索引(使用match()),m$v4使用这些索引访问元素以替换 d 矩阵,列 v2 中的元素。

于 2019-02-26T21:11:37.987 回答