Hello, In a nutshell, I've got a data.frame like this:> assignation <- data.frame(value=c(6.5,7.5,8.5,12.0),class=c(1,3,5,2)) > assignationvalue class 1 6.5 1 2 7.5 3 3 8.5 5 4 12.0 2>and a long vector of classes like this:> x <- c(1,1,2,7,6,5,4,3,2,2,2...)And would like to obtain a vector of length = length(x), with the corresponding values extracted from assignation table. Like this:> x.value[1] 6.5 6.5 12.0 NA NA 8.5 NA 7.5 12.0 12.0 12.0 Could you help me with an elegant way to do this ? (I just can do it with looping for each class in the assignation table, what a think is not perfect in R's sense) Wishes, Javier -- Javier Garc?a-Pintado Institute of Earth Sciences Jaume Almera (CSIC) Lluis Sole Sabaris s/n, 08028 Barcelona Phone: +34 934095410 Fax: +34 934110012 e-mail:jgarcia at ija.csic.es
> assignation$value[match(x,assignation$class)][1] 6.5 6.5 12.0 NA NA 8.5 NA 7.5 12.0 12.0 12.0 On 2/1/07, javier garcia-pintado <jgarcia at ija.csic.es> wrote:> Hello, > In a nutshell, I've got a data.frame like this: > > > assignation <- data.frame(value=c(6.5,7.5,8.5,12.0),class=c(1,3,5,2)) > > assignation > value class > 1 6.5 1 > 2 7.5 3 > 3 8.5 5 > 4 12.0 2 > > > > and a long vector of classes like this: > > > x <- c(1,1,2,7,6,5,4,3,2,2,2...) > > And would like to obtain a vector of length = length(x), with the > corresponding values extracted from assignation table. Like this: > > x.value > [1] 6.5 6.5 12.0 NA NA 8.5 NA 7.5 12.0 12.0 12.0 > > Could you help me with an elegant way to do this ? > (I just can do it with looping for each class in the assignation table, > what a think is not perfect in R's sense) > > Wishes, > Javier > -- > > Javier Garc?a-Pintado > Institute of Earth Sciences Jaume Almera (CSIC) > Lluis Sole Sabaris s/n, 08028 Barcelona > Phone: +34 934095410 > Fax: +34 934110012 > e-mail:jgarcia at ija.csic.es > > > ______________________________________________ > R-help at stat.math.ethz.ch 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?
one way is the following:
assignation$value[match(x, assignation$class)]
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "javier garcia-pintado" <jgarcia at ija.csic.es>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, February 01, 2007 12:05 PM
Subject: [R] indexing
> Hello,
> In a nutshell, I've got a data.frame like this:
>
>> assignation <-
>> data.frame(value=c(6.5,7.5,8.5,12.0),class=c(1,3,5,2))
>> assignation
> value class
> 1 6.5 1
> 2 7.5 3
> 3 8.5 5
> 4 12.0 2
>>
>
> and a long vector of classes like this:
>
>> x <- c(1,1,2,7,6,5,4,3,2,2,2...)
>
> And would like to obtain a vector of length = length(x), with the
> corresponding values extracted from assignation table. Like this:
>> x.value
> [1] 6.5 6.5 12.0 NA NA 8.5 NA 7.5 12.0 12.0 12.0
>
> Could you help me with an elegant way to do this ?
> (I just can do it with looping for each class in the assignation
> table,
> what a think is not perfect in R's sense)
>
> Wishes,
> Javier
> --
>
> Javier Garc?a-Pintado
> Institute of Earth Sciences Jaume Almera (CSIC)
> Lluis Sole Sabaris s/n, 08028 Barcelona
> Phone: +34 934095410
> Fax: +34 934110012
> e-mail:jgarcia at ija.csic.es
>
>
--------------------------------------------------------------------------------
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
> a <- data.frame(value=c(6.5,7.5,8.5,12.0),class=c(1,3,5,2))> x <- c(1,1,2,7,6,5,4,3,2,2,2) > match(x, a$class) [1] 1 1 4 NA NA 3 NA 2 4 4 4 > a[match(x, a$class), "value"] [1] 6.5 6.5 12.0 NA NA 8.5 NA 7.5 12.0 12.0 12.0 > -- Tony Plate javier garcia-pintado wrote:> Hello, > In a nutshell, I've got a data.frame like this: > > >>assignation <- data.frame(value=c(6.5,7.5,8.5,12.0),class=c(1,3,5,2)) >>assignation > > value class > 1 6.5 1 > 2 7.5 3 > 3 8.5 5 > 4 12.0 2 > >> > > > and a long vector of classes like this: > > >>x <- c(1,1,2,7,6,5,4,3,2,2,2...) > > > And would like to obtain a vector of length = length(x), with the > corresponding values extracted from assignation table. Like this: > >>x.value > > [1] 6.5 6.5 12.0 NA NA 8.5 NA 7.5 12.0 12.0 12.0 > > Could you help me with an elegant way to do this ? > (I just can do it with looping for each class in the assignation table, > what a think is not perfect in R's sense) > > Wishes, > Javier > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch 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.