1

Thinking I can take the easy way out, I was going to use elseif to replace id codes in an entire dataset. I have a specific dataset with a id column. I have to replace these old ids with updated ids, but there are 50k+ rows with 270 unique ids. So, I first tried:

df$id<- ifelse(df$id==  2,  1,
ifelse(df$id==  3,  5,
ifelse(df$id ==  4,  5,
ifelse(df$id==  6,  NA,
ifelse(df$id==  7,  7,
ifelse(df$id== 285, NA,
ifelse(df$id==  8,  10,.....
ifelse(df$id=200, 19, df$id)

While this would have worked, I am limited to 51 nests, and I cannot separate them because it would only a 1/4 of the set. And then updates for first half would interfere as codes do overlap.

I then tried

df$id[df$id== 2]  <- 1

and I was going to do that for every code. However, if I update all twos to one, there is still a later code in which old and new "1" will become X number, and I would only want the old "1" to become X... I actually think this takes out the if else even if 51 was not the limit. A function similar to vlookup in Excel? Any ideas?

Thanks!

Old forum related to replacing cell contents, but does not work in my case.

Replace contents of factor column in R dataframe

Community
  • 1
  • 1
sammyramz
  • 533
  • 2
  • 5
  • 13

1 Answers1

1

partial example

df <- data.frame(id=seq(1, 10))
old.id <- c(2, 3, 4, 6)
new.id <- c(1, 5, 5, NA)

df$id[df$id %in% old.id] <- new.id[unlist(sapply(df$id, function(x) which(old.id==x)))]

output

> df
   id
1   1
2   1
3   5
4   5
5   5
6  NA
7   7
8   8
9   9
10 10
Ricky
  • 4,616
  • 6
  • 42
  • 72