[R] Logical test and look up table

jim holtman jholtman at gmail.com
Sun Jan 20 01:14:16 CET 2008


Here is one way of doing it using 'match'

> # mapping table
> x <- read.table(textConnection("Source Type
+ A23  1
+ A24   2
+ A9    2
+ A32   H
+ A25   H1
+ A14   3
+ A10   4
+ A12   H1
+ A11   1
+ A13   4
+ G  h"), header=TRUE, as.is=TRUE)
>
> # generate some test data
> test <- sample(x$Source,50,T)
> # create a new dataframe with the mapping
> newdata <- data.frame(Source=test, Type=x$Type[match(test, x$Source)])
>
> newdata
   Source Type
1     A10    4
2     A13    4
3     A10    4
4     A13    4
5     A32    H
6     A12   H1
7     A12   H1
8     A10    4
9     A24    2
10    A12   H1
11    A12   H1
12    A13    4


On Jan 19, 2008 8:57 PM, Nikola MARKOV <markov at lyon.inserm.fr> wrote:
> Dear R users,
> I have a data frame with one column (4000 rows) containing name codes
> (factor with 63 levels). I would like to  associate each name with a
> particular Type (coded as 1,2,3,4,H or H1) in a second column. Is it
> possible to do a lookup table of associations (i.e. A23 is of type 1, A13
> is of type 3 ...) so as to fill up automatically the $Type column.
>
> df()
> $Source    $Type
> A23
> A24
> A9
> A32
> A25
> A14
> A10
> A12
> A11
> A13
> G
>
>
> Alternative solutions are also welcome.
> Thanks in advance
>
> P.S. I found a discussion mentioning match() and %in% but it does not seem
> adapted.
>
> --
> Nikola Markov
> Inserm U 846 Stem cells and brain research institute
> 18 av Du Doyen Lepine
> 69500 Bron
> France
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list