[R] help me understand how things work.
Michael Bedward
michael.bedward at gmail.com
Thu Sep 16 14:16:30 CEST 2010
Hello Alex,
Look at the help page for the dist function. You'll see it doesn't
return a simple vector or matrix, rather a "dist" class object which
is why you got a surprise when you tried to treat it like a simple
value. The function is not really intended for spatial point distance
calculations, but rather for multivariate analysis.
Your example would work if you did it like this...
d < dist( temp )[1]
dd <- 1 / sqrt( d )
But if you want to have more than just two points in your matrix it
will get ugly trying to work out the indices for the distances
(although the dist help page gives you a formula).
You'd be better for using a simple function...
getdist <- function(p1, p2) sqrt( (p1[1] - p2[1])^2 + (p1[2] - p2[2])^2 )
Or if you are going to do more complex point pattern analyses look at
one of the many spatial packages for R such as spatstat.
Hope this helps,
Michael
On 16 September 2010 20:02, Alaios <alaios at yahoo.com> wrote:
> Hello I have some strange output from R and I try to understand how R works.
>
> Could you please help me with that?
>
> temp <- rbind (c(10,1),c(99,98))
>> temp
> [,1] [,2]
> [1,] 10 1
> [2,] 99 98
>
>
>> dist(temp)
> 1
> 2 131.6435
>
>
>> sqrt(dist(temp))
> 1
> 2 11.47360
>
> so far so good.
>
> until the nex line: when I try to do what i did before but adding the 1/(what I
> did before). I was expecting a number as a result of the division but
> unfortunately I took the following:
>
> 1/sqrt(dist(temp))
> [1] 0.08715662
> attr(,"Size")
> [1] 2
> attr(,"Diag")
> [1] FALSE
> attr(,"Upper")
> [1] FALSE
> attr(,"method")
> [1] "euclidean"
> attr(,"call")
> dist(x = temp)
> attr(,"class")
> [1] "dist"
>
>
> Could you please help me understand what is this about?
>
> I would like to thank you in advance for your help
> Best REgards
> Alex
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>
More information about the R-help
mailing list