[R] help me understand how things work.

2010-09-16 Thread Alaios
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,]   101
[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@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.


Re: [R] help me understand how things work.

2010-09-16 Thread Michael Bedward
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 ala...@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@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.


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


Re: [R] help me understand how things work.

2010-09-16 Thread Julian Burgos
Hi Alex,

What is happening is that the ´dist´function calculates a distance matrix,
and returns an object of the ´dist´ class.

 temp - rbind (c(10,1),c(99,98))
 x=dist(temp)
 x
 1
2 131.6435
 class(x)
[1] dist

You can see a description of the ´dist´class at the end of the function´s
help file.  Do this:
?dist

So when you do dist(temp) you do not just get a number.  You get an object
that has the distance between the points, plus additional information (i.e.
the method used to calculate the distance, if the matrix contains only the
lower triangle or also the upper triangle and the diagonal, etc.).  If you
do sqrt() or 1/ over this object it is still of the ´dist´ class:

 x=dist(temp)
 x=sqrt(x)
 class(x)
[1] dist
 x=1/x
 class(x)
[1] dist

Notice that you still the right answer (0.08715662).  For some reason R
prints the attributes of the object when you do the inverse but not when you
do the square root (I´m curious about why...If anyone has an answer please
pitch in).

If you only want to get a number, do this:
 x=as.numeric(dist(temp))
 class(x)
[1] numeric
 1/sqrt(as.numeric(dist(temp)))
[1] 0.08715662

All the best,

Julián

Julian Mariano Burgos
Hafrannsóknastofnunin/Marine Research Institute
Skúlagata 4, 121 Reykjavík, Iceland
Sími/Telephone : +354-5752037
Bréfsími/Telefax:  +354-5752001
Netfang/Email: jul...@hafro.is, jmbur...@uw.edu


On Thu, Sep 16, 2010 at 10:02 AM, Alaios ala...@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,]   101
 [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@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.




--

[[alternative HTML version deleted]]

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


Re: [R] help me understand how things work.

2010-09-16 Thread Alaios
I fixed by adding this:

k - sqrt(dist(temp))
k returns sort of a list. So I need to select the first item which is the 
result.
a - k[1]

Can someone explain me why k[1] is needed for that?

Best Regards
Alex





From: Mario Valle mva...@cscs.ch

Sent: Thu, September 16, 2010 1:28:31 PM
Subject: Re: [R] help me understand how things work.

  ?dist
BTW, to me this does not happens.
x - matrix(rnorm(100), nrow=5)
d - dist(x)
1/sqrt(d)
1/sqrt(dist(x))

Hope it helps
 mario
On 16-Sep-10 12:02, Alaios 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,]   101
 [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@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.

-- 
Ing. Mario Valle
Data Analysis and Visualization Group| http://www.cscs.ch/~mvalle
Swiss National Supercomputing Centre (CSCS)  | Tel:  +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax:  +41 (91) 610.82.82


  
[[alternative HTML version deleted]]

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


Re: [R] help me understand how things work.

2010-09-16 Thread Mario Valle

 print(k) and you see a lower triangular distance matrix.
k[1] selects distances between 1 and 2
k[2] selects distances between 1 and 3 and so on.
you have a distance matrix, not a single distance value, so you have to 
select which distance you need.

Ciao!
mario

On 16-Sep-10 14:38, Alaios wrote:

I fixed by adding this:

k - sqrt(dist(temp))
k returns sort of a list. So I need to select the first item which is 
the result.

a - k[1]

Can someone explain me why k[1] is needed for that?

Best Regards
Alex


*From:* Mario Valle mva...@cscs.ch
*To:* Alaios ala...@yahoo.com
*Sent:* Thu, September 16, 2010 1:28:31 PM
*Subject:* Re: [R] help me understand how things work.

  ?dist
BTW, to me this does not happens.
x - matrix(rnorm(100), nrow=5)
d - dist(x)
1/sqrt(d)
1/sqrt(dist(x))

Hope it helps
mario
On 16-Sep-10 12:02, Alaios 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,]  101
 [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@r-project.org mailto:R-help@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.

--
Ing. Mario Valle
Data Analysis and Visualization Group| 
http://www.cscs.ch/~mvalle http://www.cscs.ch/%7Emvalle
Swiss National Supercomputing Centre (CSCS)  | Tel:  +41 (91) 
610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax:  +41 (91) 
610.82.82





--
Ing. Mario Valle
Data Analysis and Visualization Group| http://www.cscs.ch/~mvalle
Swiss National Supercomputing Centre (CSCS)  | Tel:  +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax:  +41 (91) 610.82.82

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