-2

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

我想根据另一个名为data2.

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

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

那可能吗?

例如,如果这是弧

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
4

3 回答 3

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"
于 2013-08-07T00:21:04.553 回答
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
}))
于 2013-08-07T01:20:26.990 回答
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
于 2013-08-07T00:14:25.797 回答