Документ взят из кэша поисковой машины. Адрес оригинального документа : http://herba.msu.ru/shipunov/school/biol_240/en/r/recode.r
Дата изменения: Wed Jul 15 07:16:34 2015
Дата индексирования: Mon Apr 11 03:52:59 2016
Кодировка:
# R functions for recoding
# Alexey Shipunov, dactylorhiza@gmail.com
# v. 20150714

# ===

#
# Multiply recoding
#
# if starting points ("from") are the same, only _last_ rule ("from-to" pair) has an effect
# if rules are chained, they still work independently (i.e., chaining has no effect)
#
Recode <- function(var, from, to){
x <- as.vector(var)
x.tmp <- x
for (i in 1:length(from)){x <- replace(x, x.tmp == from[i], to[i])}
if(is.factor(var)) factor(x) else x}

#
# By default, this will change non-Recode()'d entries with empty string ("")
#
Recode4 <- function(var, from, to, missed=""){ifelse(Recode(var, from, to) == var, missed, Recode(var, from, to))}

# ===

#
# Multiply running recoding
#
# if starting points ("from") are the same, only _first_ rule ("from-to" pair) has an effect
# chaining is possible
#
RecodeR <- function(var, from, to){
x <- as.vector(var)
for (i in 1:length(from)){x <- replace(x, x == from[i], to[i])}
if(is.factor(var)) factor(x) else x}

#
# By default, this will change non-RecodeR()'ed entries with empty string ("")
#
Recode4R <- function(var, from, to, missed=""){ifelse(RecodeR(var, from, to) == var, missed, RecodeR(var, from, to))}