[R] Logical test and look up table
Erin Hodgess
erinm.hodgess at gmail.com
Sun Jan 20 01:18:44 CET 2008
Hi Nicola:
Here is something to consider:
> #Fake data set
> x.df
V1 Type
1 A23 M
2 A24 N
3 A9 Y
4 A32 E
5 A25 R
6 A14 V
7 A10 G
8 A12 W
9 A11 J
10 A13 Q
11 G O
> #new set
> y.df
Source
1 A10
2 A32
3 A10
4 A25
5 A32
6 G
7 A24
8 A32
9 A32
10 A23
11 A23
12 A32
13 A10
14 A14
15 A10
16 A25
17 A23
18 A25
19 A14
20 A32
21 A9
22 A12
23 A14
24 A14
25 A25
26 A23
27 A11
28 A25
29 A12
30 A24
> y.df$Type <- x.df$Type[match(y.df$Source,x.df$V1)]
> y.df
Source Type
1 A10 G
2 A32 E
3 A10 G
4 A25 R
5 A32 E
6 G O
7 A24 N
8 A32 E
9 A32 E
10 A23 M
11 A23 M
12 A32 E
13 A10 G
14 A14 V
15 A10 G
16 A25 R
17 A23 M
18 A25 R
19 A14 V
20 A32 E
21 A9 Y
22 A12 W
23 A14 V
24 A14 V
25 A25 R
26 A23 M
27 A11 J
28 A25 R
29 A12 W
30 A24 N
>
On Jan 19, 2008 7: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.
>
--
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: erinm.hodgess at gmail.com
More information about the R-help
mailing list