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)]) > > newdataSource 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?
Hi Nicola: Here is something to consider:> #Fake data set > x.dfV1 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.dfSource 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.dfSource 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
here is another one. x <- read.table(textConnection(" id source 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"), header = TRUE, as.is = TRUE); y <- read.table(textConnection(" id 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"), header = TRUE, as.is = TRUE); library(sqldf); wanted <- sqldf("select x.source, x.type from x inner join y on x.source = y.source"); 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. >-- ==============================WenSui Liu Statistical Project Manager ChoicePoint Precision Marketing (http://spaces.msn.com/statcompute/blog)
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