-1

I have two R data files (rda format), the first one is an edge list and contains only id numbers, the second one contains ids and names. I need to match the names from the second file to the ids in the first one, or just replace them. Which command should I use for that?

Here is how the first file looks like:

one <- data.frame(X1=c("id1","id1"),X2=c("id2","id3"))

    X1  X2
1   id1 id2
2   id1 id3

Here is the second one:

two <- data.frame(User=c("g79","kian","greyracer"),ID=c("id1","id2","id3"))

    User      ID
1   g79       id1
2   kian      id2
3   greyracer id3

Thanks in advance!

thelatemail
  • 85,757
  • 12
  • 122
  • 177
Tash
  • 53
  • 1
  • 6

2 Answers2

1

I think merge would go a long way in solving this problem. See ?merge for more details. Without a reproducible example it is hard for me to provide a concrete example.

Community
  • 1
  • 1
Paul Hiemstra
  • 58,502
  • 12
  • 138
  • 147
1

I'm sure there is a simpler way, but this will do it:

one <- data.frame(X1=c("id1","id1"),X2=c("id2","id3"))
two <- data.frame(User=c("g79","kian","greyracer"),ID=c("id1","id2","id3"))

data.frame(lapply(one, function(x) two$User[match(x,two$ID)]))

   X1        X2
1 g79      kian
2 g79 greyracer
thelatemail
  • 85,757
  • 12
  • 122
  • 177
  • Thanks. Still have a question: how do i address the data files in my directory, so that I don't type out all my observations? I tried this, but it didn't work: edgelist – Tash Jun 03 '13 at 11:12
  • If they are rda files you can just do `one – thelatemail Jun 03 '13 at 11:16
  • Now I get an Error in map$User : $ operator is invalid for atomic vectors. What shall I do? – Tash Jun 03 '13 at 11:23
  • When I try to execute the command like you wrote before, I get the following: `code`edgelist Error in map$User : $ operator is invalid for atomic vectors I tried the other way and rewrote the last line, but still unsuccessfully: `code`data.frame(lapply(edgelist, function(x) map['User'][match(x,map['ID'])])) > NA_character_. 1 – Tash Jun 03 '13 at 13:00
  • @Natalia - i'm guessing either `map` or `edgelist` are a matrix, not a `data.frame` as in my answer. Try `is.matrix(map)` to check. Then use `as.data.frame(map)` to change it. This guesswork could be eliminated if you followed @Paul's advice below and provided a reproducible example. – thelatemail Jun 03 '13 at 20:22