[R] problems with subset (misunderstanding somewhere)

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 5 14:35:36 CEST 2005


On Tue, 5 Apr 2005, Wladimir Eremeev wrote:

> Dear r-help,
>
> I have the following function defined:
>
> cubic.distance<-function(x1,y1,z1,x2,y2,z2) {
>  max(c(abs(x1-x2),abs(y1-y2),abs(z1-z2)))
> }
>
> I have a data frame from which I make subsets.
>
> When I call
>  subset(dataframe,cubic.distance(tb19h,tb37v,tb19v,190,210,227)<=2)
> I have the result with 0 rows.
>
> However, the data frame contains the row (among others, that suit)
> tb19v tb19h tb37v
> 226.6 189.3 208.4
>
> Call
>   cubic.distance(189.3,208.4,226.6,190,210,227)
> gives
> [1] 1.6
>
> Next call:
>> cubic.distance(189.3,208.4,226.6,190,210,227)<=2
> [1] TRUE
>
> It seems to me, that I have made errors somewhere in calls.
> Could you, please, be so kind, to tell me, where they are?

Your function finds the maximum distance over all rows (it is passed 
vectors).  Replace max() by pmax() for a logical result for each row.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list