[R] Equivalents of Matlab's 'find' and 'end'
Gavin Simpson
gavin.simpson at ucl.ac.uk
Thu Oct 7 16:38:24 CEST 2004
Bryan L. Brown wrote:
> Sorry if these questions have been asked recently--I'm new to this
> list.
Hi Bryan,
A useful resource is Robin Hankin's contributed document "R and Octave":
http://cran.r-project.org/doc/contrib/R-and-octave-2.txt
> The first is Matlab's 'find' command. This is one of the most useful
> commands in Matab. Basically, if X is the vector
>
> X=[3, 2, 1, 1, 2, 3]
>
> the command
>
> 'find(X==1)'
>
> would return the vector [3, 4]
> x <- c(3, 2, 1, 1, 2, 3)
> which(x == 1)
[1] 3 4
>
> The second Matlab command that I'd like to find an R equivalent for
> is 'end'. 'end' is just a simple little command that indicates the
> end of a row/column. It is incredibly handy when used to subset
> matrices like
>
> Y = X(2:end)
>
> and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the
> previous example.
> Y <- x[-1]
> Y
[1] 2 1 1 2 3
Not quite the same I'll grant you, but results in the same thing. If you
wanted Y = X(4:end) you could use:
> Y <- x[-c(1:3)]
> Y
[1] 1 2 3
HTH
Gav
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson [T] +44 (0)20 7679 5522
ENSIS Research Fellow [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC [E] gavin.simpson at ucl.ac.uk
UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/
London. WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list