lookup: Recode several values of a variable

Description Usage Arguments Details Author(s) See Also Examples

View source: R/epiDisplay.R

Description

Systematic replacement of several values of a variable using an array

Usage

1
lookup(x, lookup.array) 

Arguments

x

a variable

lookup.array

a n-by-2 array used for looking up the recoding scheme

Details

This command is used for changing more than one value of a variable using a n-by-2 look-up array. The first column of the look-up array (index column) must be unique.

If either the variable or the look-up table is character, the result vector will be character.

For changing the levels of a factor variable, 'recode(vars, "old level", "new level")' or 'levels(var) <- ' instead.

Author(s)

Virasakdi Chongsuvivatwong <[email protected]>

See Also

'replace', 'recode'

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
a       <- c( 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, NA)
tx      <- rbind(c(1,2),c(2,1),c(3,4),c(4,NA),c(NA,3)) 

# Swapping values of 1 and 2; rotating 3, 4 and NA
new.a  <- lookup(a, tx)
data.frame(a, new.a)
tableA <- table(a, new.a, exclude=NULL) 
# All non-diagonal cells which are non-zero are the recoded cells. 
print(tableA, zero=".")

## Character look-up table
b <- c(rep(letters[1:4],2), ".", NA)
tx1 <- cbind(c(letters[1:5], ".", NA), c("Disease A","Disease B","Disease C", 
	"Disease D","Disease E", NA, "Unknown"))
DiseaseName <- lookup(b, tx1)
data.frame(b, DiseaseName)

Example output

Loading required package: foreign
Loading required package: survival
Loading required package: MASS
Loading required package: nnet
    a new.a
1   1     2
2   2     1
3   2     1
4   3     4
5   3     4
6   3     4
7   4    NA
8   4    NA
9   4    NA
10  4    NA
11  5     5
12  5     5
13  5     5
14  5     5
15  5     5
16 NA     3
      new.a
a      1 2 3 4 5 <NA>
  1    . 1 . . .    .
  2    2 . . . .    .
  3    . . . 3 .    .
  4    . . . . .    4
  5    . . . . 5    .
  <NA> . . 1 . .    .
      b DiseaseName
1     a   Disease A
2     b   Disease B
3     c   Disease C
4     d   Disease D
5     a   Disease A
6     b   Disease B
7     c   Disease C
8     d   Disease D
9     .        <NA>
10 <NA>     Unknown

epiDisplay documentation built on May 11, 2018, 1:04 a.m.