自动替换数据框中的名称

时间:2013-08-07 00:09:47

标签: r

我在R中有一个名为arcs的数据框,其中包含一些我希望更改其名称的值。

我想根据另一个名为data2的数据框更改名称。

data2有两列,第一列是旧名,第二列是新名。

我想通过arcs,如果arcs中的名称出现在data2的第一列,我想将其更改为第二列data2

这可能吗?

例如,如果这是arcs

data 1    blah 1
blah 2    data 2
blah 3    data 3
blah 4    blah 5
data 4    data 5

这是data2

data 1    real 1
data 2    real 2
data 3    real 3
data 4    real 4
data 5    real 5

我希望新的弧看起来像这样:

real 1    blah 1
blah 2    real 2
blah 3    real 3
blah 4    blah 5
real 4    real 5

3 个答案:

答案 0 :(得分:3)

假设您的数据都是matrix结构而不是data.frames(发表评论),请尝试以下操作:

arcs <- matrix(c("data1","blah2","blah3","blah4","data4",
                 "blah1","data2","data3","blah5","data5"),ncol=2)
data2 <- matrix(c("data1","data2","data3","data4","data5",
                 "real1","real2","real3","real4","real5"),ncol=2)

> arcs
#     [,1]    [,2]   
#[1,] "data1" "blah1"
#[2,] "blah2" "data2"
#[3,] "blah3" "data3"
#[4,] "blah4" "blah5"
#[5,] "data4" "data5"
> data2
#     [,1]    [,2]   
#[1,] "data1" "real1"
#[2,] "data2" "real2"
#[3,] "data3" "real3"
#[4,] "data4" "real4"
#[5,] "data5" "real5"

解决问题:

matches <- match(arcs,data2[,1])
arcs[!is.na(matches)] <- data2[,2][matches[!is.na(matches)]]

结果:

> arcs
#     [,1]    [,2]   
#[1,] "real1" "blah1"
#[2,] "blah2" "real2"
#[3,] "blah3" "real3"
#[4,] "blah4" "blah5"
#[5,] "real4" "real5"

答案 1 :(得分:2)

thelatemail的答案很好,但只适用于矩阵。这是一个适用于data.frames的解决方案,即具有多种类型的列。我正在使用%in%(类似于match)并循环遍历所有弧列。

do.call(cbind,lapply(arcs,function(x){
       x[x %in% data2$V1] <- 
         data2$V2[x %in% data2$V1]
       x
}))

答案 2 :(得分:0)

arcs<-structure(list(col1 = structure(c(4L, 1L, 2L, 3L, 5L), .Label = c("blah 2", 
"blah 3", "blah 4", "data 1", "data 4"), class = "factor"), col2 = structure(c(1L, 
3L, 4L, 2L, 5L), .Label = c("blah 1", "blah 5", "data 2", "data 3", 
"data 5"), class = "factor")), .Names = c("col1", "col2"), class = "data.frame", row.names = c(NA, 
-5L))

> arcs
    col1   col2
1 data 1 blah 1
2 blah 2 data 2
3 blah 3 data 3
4 blah 4 blah 5
5 data 4 data 5

data2<-structure(list(oldname = structure(1:5, .Label = c("data 1", 
"data 2", "data 3", "data 4", "data 5"), class = "factor"), newname = structure(1:5, .Label = c("real 1", 
"real 2", "real 3", "real 4", "real 5"), class = "factor")), .Names = c("oldname", 
"newname"), class = "data.frame", row.names = c(NA, -5L))

 > data2
  oldname newname
1  data 1  real 1
2  data 2  real 2
3  data 3  real 3
4  data 4  real 4
5  data 5  real 5

arcs$col1<-ifelse(as.character(arcs$col1)==as.character(data2$oldname),as.character(data2$newname),as.character(arcs$col1))
> arcs$col1
[1] "real 1" "blah 2" "blah 3" "blah 4" "data 4"

arcs$col2<-ifelse(as.character(arcs$col2)==as.character(data2$oldname),as.character(data2$newname),as.character(arcs$col2))
> arcs$col2
[1] "blah 1" "real 2" "real 3" "blah 5" "real 5"

>arcs
    col1   col2
1 real 1 blah 1
2 blah 2 real 2
3 blah 3 real 3
4 blah 4 blah 5
5 data 4 real 5