-2

I have a data frame in R, called arcs, that has some values of which I would like to change the name.

I would like to change the name based on another data frame, called data2.

data2 has two columns, the first column is the old name, and the second column is the new name.

I would like to go through arcs and if there is a name in arcs that appears in the first column of data2, I would like to change it to the second column of data2.

Is that possible?

For example, if this was arcs

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

and this was data2

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

I would want the new arcs to look like this:

real 1    blah 1
blah 2    real 2
blah 3    real 3
blah 4    blah 5
real 4    real 5
  • 1
    This doesn't sound that difficult, but can you provide some *simple* example data that represents your problem. A handful of (even made up) cases in `arcs` or `names` would do. – thelatemail Aug 07 '13 at 00:14
  • http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Henrik Aug 07 '13 at 00:14
  • Can you please clarify if you have a `data.frame` or a `matrix` for your data? The results of `is.data.frame(arcs)` and `is.matrix(arcs)` will tell you, and will allow answers to be clarified. – thelatemail Aug 07 '13 at 00:54
  • 1
    If you have a dataframe, then you probably also have these as factors. Unless you clarify with str(dfrms) or show how to build an example, you are just wasting people's time. – IRTFM Aug 07 '13 at 01:20

3 Answers3

3

Assuming your data are both matrix structures rather than data.frames (going on your comments), then try this:

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"

Fix it up:

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

Result:

> arcs
#     [,1]    [,2]   
#[1,] "real1" "blah1"
#[2,] "blah2" "real2"
#[3,] "blah3" "real3"
#[4,] "blah4" "blah5"
#[5,] "real4" "real5"
thelatemail
  • 91,185
  • 12
  • 128
  • 188
2

The answer of thelatemail is good but works only with matrix. Here a solution that works with data.frames ie with multi types columns. I am using %in% (similar to match) and looping through all the columns of arcs.

do.call(cbind,lapply(arcs,function(x){
       x[x %in% data2$V1] <- 
         data2$V2[x %in% data2$V1]
       x
}))
agstudy
  • 119,832
  • 17
  • 199
  • 261
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
Metrics
  • 15,172
  • 7
  • 54
  • 83