# [R] Cutting vectors

Ben Bolker ben at zoo.ufl.edu
Mon Apr 3 04:56:16 CEST 2000

```  Without being sure, I think what you're trying to do is achieved by
match().

Example:

> x <- factor(1:4)
> y <- c(7,5,3,8)
> z <- c(2,3,1)
> y[z==x]
numeric(0)
Warning messages:
1: longer object length
is not a multiple of shorter object length in: is.na(e1) | is.na(e2)
2: longer object length
is not a multiple of shorter object length in: ==.default(z, x)
> y[match(z,x)]
[1] 5 3 7

Question: what cross-references in the documentation would have helped
you find this?  What keywords would you have looked for?
I'm vaguely thinking about putting together a list of "How do I ... in
R?"

Ben

On Sun, 2 Apr 2000, Patrik Waldmann wrote:

>
> > Patrik Waldmann wrote: > > > I have one factor with say five levels
> (fact) of different size and a corresponding variable (var). I
> randomly generate five new levels (nfact ex. 2,2,1,5,4). What I would
> like to do is to extract the corresponding variable values into a new
> variable (nvar). I did try the following, why doesn't it work?
> nvar<-var[fact==nfact[ ]] > > ..I ain't sure I get evrything but try >
> > > nvar<-var[which(fact == nfact)] >
>
> Gives me the same error message as before (and creates a meaningless vector):
>
> Warning messages:
> 1: longer object length
>         is not a multiple of shorter object length in: is.na(e1) | is.na(e2)
> 2: longer object length
>         is not a multiple of shorter object length in: ==.default(fact, nfact)
>
> Has this something to with the non-equal size of the 'cuttings'?
>
> Patrik
>
>
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
>

--
Ben Bolker                                  bolker at zoo.ufl.edu
Zoology Department, University of Florida   http://www.zoo.ufl.edu/bolker
318 Carr Hall/Box 118525                    tel: (352) 392-5697
Gainesville, FL 32611-8525                  fax: (352) 392-3704

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```