Skip to content

Doing dist on separate objects in a text file

5 messages · R. Michael Weylandt, David Winsemius, ScottDaniel +1 more

#
So I have a text file that looks like this:
	"Label"	"X"	"Y"	"Slice"
1	"Field_1_R3D_D3D_PRJ_w617.tif"	348	506	1
2	"Field_1_R3D_D3D_PRJ_w617.tif"	359	505	1
3	"Field_1_R3D_D3D_PRJ_w617.tif"	356	524	1
4	"Field_1_R3D_D3D_PRJ_w617.tif"	2	0	1
5	"Field_1_R3D_D3D_PRJ_w617.tif"	412	872	1
6	"Field_1_R3D_D3D_PRJ_w617.tif"	422	863	1
7	"Field_1_R3D_D3D_PRJ_w617.tif"	429	858	1
8	"Field_1_R3D_D3D_PRJ_w617.tif"	429	880	1
9	"Field_1_R3D_D3D_PRJ_w617.tif"	437	865	1
10	"Field_1_R3D_D3D_PRJ_w617.tif"	447	855	1
11	"Field_1_R3D_D3D_PRJ_w617.tif"	450	868	1
12	"Field_1_R3D_D3D_PRJ_w617.tif"	447	875	1
13	"Field_1_R3D_D3D_PRJ_w617.tif"	439	885	1
14	"Field_1_R3D_D3D_PRJ_w617.tif"	2	8	1

What it represents are the locations of centromeres per nucleus in a
microscope image. What I need to do is do a dist() on each grouping (the
grouping being separated by the low values of x and y's) and then compute an
average. The part that I'm having trouble with is writing code that will
allow R to separate these objects. Do I have to find some way of creating
separate data frames for each object? Or is there a way to parse the file
and generate a single data frame of all the pairwise distances? Any
suggestions or example code would be much appreciated. Thanks!

--
View this message in context: http://r.789695.n4.nabble.com/Doing-dist-on-separate-objects-in-a-text-file-tp3994515p3994515.html
Sent from the R help mailing list archive at Nabble.com.
#
Perhaps split() directly or more abstractly tapply() from base or one
of the d_ply() from plyr?

Michael
On Sat, Nov 5, 2011 at 7:20 PM, ScottDaniel <scottdaniel25 at gmail.com> wrote:
#
On Nov 5, 2011, at 7:20 PM, ScottDaniel wrote:

            
I'm having trouble figuring out what you mean by "separating the  
objects". Each row is a separate reading, and I think you just want  
pairwise distances, right?
I don't think so. You need to read this file into a data.frame which  
should be fairly trivial with read.table is you specify the  
header=TRUE parameter.
Then assuming there is now a data.frame named "dat" with those values:

dist( cbind(dat$X, dat$Y))

One stumbling block might have been recognizing that the dist function  
will not work with two x and y arguments but rather requires a matrix  
(or something coercible to a matrix)  as its first argument. This  
would also have worked:

dist(dat[ , c("X", "Y")])
#
Is this what you want:
+ 1   "Field_1_R3D_D3D_PRJ_w617.tif"  348     506     1
+ 2   "Field_1_R3D_D3D_PRJ_w617.tif"  359     505     1
+ 3   "Field_1_R3D_D3D_PRJ_w617.tif"  356     524     1
+ 4   "Field_1_R3D_D3D_PRJ_w617.tif"  2       0       1
+ 5   "Field_1_R3D_D3D_PRJ_w617.tif"  412     872     1
+ 6   "Field_1_R3D_D3D_PRJ_w617.tif"  422     863     1
+ 7   "Field_1_R3D_D3D_PRJ_w617.tif"  429     858     1
+ 8   "Field_1_R3D_D3D_PRJ_w617.tif"  429     880     1
+ 9   "Field_1_R3D_D3D_PRJ_w617.tif"  437     865     1
+ 10  "Field_1_R3D_D3D_PRJ_w617.tif"  447     855     1
+ 11  "Field_1_R3D_D3D_PRJ_w617.tif"  450     868     1
+ 12  "Field_1_R3D_D3D_PRJ_w617.tif"  447     875     1
+ 13  "Field_1_R3D_D3D_PRJ_w617.tif"  439     885     1
+ 14  "Field_1_R3D_D3D_PRJ_w617.tif"  2       8       1'), header =
TRUE, as.is = TRUE)
$`0`
         1        2
2 11.04536
3 19.69772 19.23538

$`1`
           5         6         7         8         9        10
11        12
6  13.453624
7  22.022716  8.602325
8  18.788294 18.384776 22.000000
9  25.961510 15.132746 10.630146 17.000000
10 38.910153 26.248809 18.248288 30.805844 14.142136
11 38.209946 28.442925 23.259407 24.186773 13.341664 13.341664
12 35.128336 27.730849 24.758837 18.681542 14.142136 20.000000
7.615773
13 29.966648 27.802878 28.792360 11.180340 20.099751 31.048349
20.248457 12.806248
On Sun, Nov 6, 2011 at 3:49 PM, ScottDaniel <scottdaniel25 at gmail.com> wrote: