[R] indexing without looping

2007-02-01 Thread javier garcia-pintado
Hello,
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:[EMAIL PROTECTED] 

__
R-help@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.


Re: [R] indexing without looping

2007-02-01 Thread David Barron
One way would be to use merge, like this:

merge(assignation,data.frame(class=x),all.y=TRUE)

There might well be better ways...

On 01/02/07, javier garcia-pintado [EMAIL PROTECTED] wrote:
 Hello,
 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:[EMAIL PROTECTED]


 __
 R-help@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.




-- 
=
David Barron
Said Business School
University of Oxford
Park End Street
Oxford OX1 1HP

__
R-help@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.


Re: [R] indexing without looping

2007-02-01 Thread Ido M. Tamir
Hi,

xt - assignation$value[match(x,assignation$class)]

HTH
ido

__
R-help@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.


Re: [R] indexing without looping

2007-02-01 Thread Greg Johnson
javier garcia-pintado jgarcia at ija.csic.es writes:

 
 Hello,
 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,

you might try 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

 x - c(1,1,2,7,6,5,4,3,2,2,2)
 x
 [1] 1 1 2 7 6 5 4 3 2 2 2

 merge( x, assignation, by.x=1, by.y=2, all.x=T )
   x value
1  1   6.5
2  1   6.5
3  2  12.0
4  2  12.0
5  2  12.0
6  2  12.0
7  3   7.5
8  4NA
9  5   8.5
10 6NA
11 7NA

__
R-help@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.