[R] Doing dist on separate objects in a text file

David Winsemius dwinsemius at comcast.net
Sun Nov 6 15:14:12 CET 2011


On Nov 5, 2011, at 7:20 PM, ScottDaniel wrote:

> 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.

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?

> Do I have to find some way of creating
> separate data frames for each object?

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.

> Or is there a way to parse the file
> and generate a single data frame of all the pairwise distances?

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")])

-- 
David.

> 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.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list