[R] replace values in data frame

Marc Schwartz (via MN) mschwartz at mn.rr.com
Fri Jul 7 19:35:45 CEST 2006


Wade,

Given that you appear to have multiple search and replace items to deal
with, here is a possible loop based "Global Search and Replace"
solution:

gsr <- function(Source, Search, Replace)
{
  if (length(Search) != length(Replace))
    stop("Search and Replace Must Have Equal Number of Items\n")

  Changed <- as.character(Source)

  for (i in 1:length(Search))
  {
    cat("Replacing: ", Search[i], " With: ", Replace[i], "\n")
    Changed <- replace(Changed, Changed == Search[i], Replace[i])
  }

  cat("\n")
  
  Changed
}


Source:  The source vector, which will be coerced to character

Search: The Search values to be matched in "Source" as a character
vector

Replace: The values that will replace those found in 'Search' on a
one-for-one basis.


This function returns a character vector. As with replace(), the result
must be assigned. The change is not done 'in place'. The function will
also output the search and replace values to the console during the
loop.


Again, using the iris dataset as an example:

> iris$Species
  [1] setosa     setosa     setosa     setosa     setosa     setosa    
  [7] setosa     setosa     setosa     setosa     setosa     setosa    
 [13] setosa     setosa     setosa     setosa     setosa     setosa    
 [19] setosa     setosa     setosa     setosa     setosa     setosa    
 [25] setosa     setosa     setosa     setosa     setosa     setosa    
 [31] setosa     setosa     setosa     setosa     setosa     setosa    
 [37] setosa     setosa     setosa     setosa     setosa     setosa    
 [43] setosa     setosa     setosa     setosa     setosa     setosa    
 [49] setosa     setosa     versicolor versicolor versicolor versicolor
 [55] versicolor versicolor versicolor versicolor versicolor versicolor
 [61] versicolor versicolor versicolor versicolor versicolor versicolor
 [67] versicolor versicolor versicolor versicolor versicolor versicolor
 [73] versicolor versicolor versicolor versicolor versicolor versicolor
 [79] versicolor versicolor versicolor versicolor versicolor versicolor
 [85] versicolor versicolor versicolor versicolor versicolor versicolor
 [91] versicolor versicolor versicolor versicolor versicolor versicolor
 [97] versicolor versicolor versicolor versicolor virginica  virginica 
[103] virginica  virginica  virginica  virginica  virginica  virginica 
[109] virginica  virginica  virginica  virginica  virginica  virginica 
[115] virginica  virginica  virginica  virginica  virginica  virginica 
[121] virginica  virginica  virginica  virginica  virginica  virginica 
[127] virginica  virginica  virginica  virginica  virginica  virginica 
[133] virginica  virginica  virginica  virginica  virginica  virginica 
[139] virginica  virginica  virginica  virginica  virginica  virginica 
[145] virginica  virginica  virginica  virginica  virginica  virginica 
Levels: setosa versicolor virginica


Note that iris$Species is a factor with specific levels. The gsr()
function above will coerce the Source argument to a character vector
internally and return a character vector:

> iris$Species <- gsr(iris$Species, 
                      c("setosa", "versicolor", "virginica"), 
                      c("s1", "v1", "v2"))
Replacing:  setosa  With:  s1 
Replacing:  versicolor  With:  v1 
Replacing:  virginica  With:  v2 

> iris$Species
  [1] "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1"
 [14] "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1"
 [27] "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1"
 [40] "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "v1" "v1"
 [53] "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1"
 [66] "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1"
 [79] "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1"
 [92] "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v2" "v2" "v2" "v2"
[105] "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2"
[118] "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2"
[131] "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2"
[144] "v2" "v2" "v2" "v2" "v2" "v2" "v2"


HTH,

Marc Schwartz



More information about the R-help mailing list