[R] Doing dist on separate objects in a text file
jim holtman
jholtman at gmail.com
Mon Nov 7 20:37:35 CET 2011
Is this what you want:
> x <- read.table(textConnection('"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'), header =
TRUE, as.is = TRUE)
> closeAllConnections()
> # create column to segment the data
> x$mark <- cumsum(x$X < 10)
> # now remove separators
> x <- subset(x, X >= 10)
> # now split the data
> x.s <- split(x, x$mark)
> # now do the dist
> lapply(x.s, function(a) dist(a[, c("X", "Y")]))
$`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:
>>
>> 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?
>
> What I mean is that rows 1-3 represent one group of centromeres and
> rows 5-13 represent a second group. So I want to do a separate dist on
> each group (i.e. I want a pair wise distance for rows 1 and 3 but not
> 1 and 12). Does that clear thing up?
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Doing-dist-on-separate-objects-in-a-text-file-tp3994515p3996701.html
> Sent from the R help mailing list archive at Nabble.com.
> [[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.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list