I'm new to R. Having read through a good chunk of "An Introduction to R" I'm trying to use R to do some relatively straight-forward (mathematically speaking) stuff. I have a file which contains the coordinates of a set of points in R^3, with one point per line (i.e. three real numbers). The aim is to read that file in and produce the set of all the distances between all possible different pairs of points in that file. So I try:> a <- read.table("FL_BR14.xyz")> dim(a)[1] 329 3> A <- function(i) a[i, ]> A(1)V1 V2 V3 1 -1558.364 16.97055 1004.381> d <- function(x, y) sqrt(sum((x - y) * (x - y)))> outer(1:length(a[, 1]), 1:length(a[, 1]), function(u,v) d(A(u), A(v))) Error in outer(1:length(a[, 1]), 1:length(a[, 1]), function(u, v) d(A(u), : dim<- : dims [product 108241] do not match the length of object [1] This last error has me stumped. Could anyone give me a pointer in the right direction? -- Gaute Strokkenes http://www.srcf.ucam.org/~gs234/ I'm CONTROLLED by the CIA!! EVERYONE is controlled by the CIA!!
?dist Rule 1: Do not try to do it by yourself. Assume that a function already exists to do what you want unless you know otherwise. Then use R's search and help capabilities to find it (it may be in an external package on CRAN). In this case, help.search("distance") would have given you dist. -- Bert Gunter Genentech
Your function is not vectorized. See the FAQ (http://cran.r-project.org/doc/FAQ/R-FAQ.html), question 7.19. -roger Gaute B Strokkenes wrote:> I'm new to R. Having read through a good chunk of "An Introduction to > R" I'm trying to use R to do some relatively straight-forward > (mathematically speaking) stuff. > > I have a file which contains the coordinates of a set of points in > R^3, with one point per line (i.e. three real numbers). The aim is to > read that file in and produce the set of all the distances between all > possible different pairs of points in that file. So I try: > > >>a <- read.table("FL_BR14.xyz") > > >>dim(a) > > [1] 329 3 > > >>A <- function(i) a[i, ] > > >>A(1) > > V1 V2 V3 > 1 -1558.364 16.97055 1004.381 > > >>d <- function(x, y) sqrt(sum((x - y) * (x - y))) > > >>outer(1:length(a[, 1]), 1:length(a[, 1]), function(u, > > v) d(A(u), A(v))) > Error in outer(1:length(a[, 1]), 1:length(a[, 1]), function(u, v) d(A(u), : > dim<- : dims [product 108241] do not match the length of object [1] > > This last error has me stumped. Could anyone give me a pointer in the > right direction? >
On Thu, 26 Aug 2004, Gaute B Strokkenes wrote:> I'm new to R. Having read through a good chunk of "An Introduction to > R" I'm trying to use R to do some relatively straight-forward > (mathematically speaking) stuff. > > I have a file which contains the coordinates of a set of points in > R^3, with one point per line (i.e. three real numbers). The aim is to > read that file in and produce the set of all the distances between all > possible different pairs of points in that file. So I try:outer() is maybe more difficult than need be, unless this isn't the real question. Perhaps look at dist() first - it solves your declared problem directly in a single command, but that makes me think your question was actually about outer, not distances? Roger> > > a <- read.table("FL_BR14.xyz") > > > dim(a) > [1] 329 3 > > > A <- function(i) a[i, ] > > > A(1) > V1 V2 V3 > 1 -1558.364 16.97055 1004.381 > > > d <- function(x, y) sqrt(sum((x - y) * (x - y))) > > > outer(1:length(a[, 1]), 1:length(a[, 1]), function(u, > v) d(A(u), A(v))) > Error in outer(1:length(a[, 1]), 1:length(a[, 1]), function(u, v) d(A(u), : > dim<- : dims [product 108241] do not match the length of object [1] > > This last error has me stumped. Could anyone give me a pointer in the > right direction? > >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no