-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
  • 91,185
  • 12
  • 128
  • 188
Tash
  • 53
  • 1
  • 7

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
  • 59,984
  • 12
  • 142
  • 149
  • Thank you Paul. The problem is that the variable names are different. So I use the command: map <- data.frame("map.rda") m1 <- merge(edgelist, map, by.x = "ID") and get an error: Error in fix.by(by.x, x) : 'by' must specify uniquely valid column(s) – Tash Jun 03 '13 at 10:50
  • Natalia, column names differ per data.frame (ID vs X2). See answer by @thelatemail. – Roman Luštrik Jun 03 '13 at 10:54
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
  • 91,185
  • 12
  • 128
  • 188
  • 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<-data.frame("edgelist_one_mode.rda") map <- data.frame("map.rda") edgelist <- data.frame("X1","X2") map <- data.frame("User", "ID") data.frame(lapply(edgelist, function(x) map$User[match(x,map$ID)])) – Tash Jun 03 '13 at 11:12
  • If they are rda files you can just do `one <- load("map.rda")` for each of the files (assuming you are working in the right directory). I was just manually recreating your data so that I had an example to show you. – 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 <- load("edgelist_one_mode.rda") `code`map <- load("map.rda") `code`data.frame(lapply(edgelist, function(x) map$User[match(x,map$ID)])) > 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