Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. recode <- function(x, old, new) {
  2.     u <- rep(NA, length(x))
  3.     for (i in 1:length(old)) {
  4.         if(is.na(old[i])) {
  5.             u[is.na(x)] <- new[i]
  6.         } else {
  7.         u[x == old[i]] <- new[i]
  8.         }
  9.     }
  10.     return(ifelse(x %in% old, u, x))
  11. }
  12.  
  13. a <- sample(c("A", "B", "C", "D", NA), 20, replace = T)
  14. recode(a, old = c("A", NA), new = c("E", "F"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement