Kaye11 - 1 month ago 5x

R Question

I have a data table object:

`> dput(head(trackdatacompvar))`

structure(list(wellvid = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A4-009",

"B3-006", "B4-015", "C2-009", "C2-034", "C3-017", "C4-014", "C4-016",

"C4-026", "C4-036"), class = "factor"), TRACK_ID = c(0L, 0L,

0L, 0L, 0L, 0L), treatment = structure(c(2L, 2L, 2L, 2L, 2L,

2L), .Label = c("Si_induced", "Si_notinduced"), class = "factor"),

A = c(0L, 0L, 0L, 0L, 0L, 0L), X = c(50.216, 50.216, 50.091,

50.091, 50.216, 50.216), Y = c(295.609, 295.609, 295.477,

295.477, 295.609, 295.609), T = 0:5, V = c(0, 0, 0.181793839279557,

0, 0.181793839279557, 0), x_grpA = c(641.67, 641.67, 641.67,

641.67, 641.67, 641.67), y_grpA = c(625, 625, 625, 625, 625,

625), rad_grpA = c(50L, 50L, 50L, 50L, 50L, 50L), x_grpB = c(889.58,

889.58, 889.58, 889.58, 889.58, 889.58), y_grpB = c(377.08,

377.08, 377.08, 377.08, 377.08, 377.08), rad_grpB = c(20L,

20L, 20L, 20L, 20L, 20L)), .Names = c("wellvid", "TRACK_ID",

"treatment", "A", "X", "Y", "T", "V", "x_grpA", "y_grpA", "rad_grpA",

"x_grpB", "y_grpB", "rad_grpB"), sorted = "wellvid", class = c("data.table",

"data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x0000000000210788>)

I want to define 4 groups of data depending on circular area. Groups A and B will be dependent on the x,y origin of 2 beads (labelled as x_grpA, y_grpA and x_grpB, y_grpB), group C is an outside area and group D as the area where groups A and B overlap (but this area is sometimes not there). The 2 circular groups should be inside a circular area with radius of 115 µm. This 115 µm is dependent on the size of the bead, so I also have in my data 2 radius (rad_grpA and rad_grpB). To understand it visually, here are 2 pictures:

My original idea is to reuse the awesome script I was given before. So, I tried defining the center of the each data point and the corresponding length of the whole area of group A as:

`center_grpA <- c(trackdatacompvar$x_grpA, trackdatacompvar$y_grpA)`

circle_grpA <- (trackdatacompvar$rad_grpA)*2 + 115

But after this I am lost.

In the end I want to put inside my dataframe their grouping as one variable.

Would appreciate any help! Thanks :)

Answer

We can use a little convenience function from a package of mine here:

```
check_if_in_circle <- function(points, x, y, r) {
(points[, 1] - x) ^ 2 + (points[, 2] - y) ^ 2 < r ^ 2
}
```

Now we check for each point, whether it's in circle A, circle B, and then `ifelse`

to figure out whether to assign A, B, C or D. I use `within`

to avoid typing that long data name.

```
trackdatacompvar <- within(trackdatacompvar,
{
grpA <- check_if_in_circle(points = cbind(X, Y),
x_grpA, y_grpA, rad_grpA + 115)
grpB <- check_if_in_circle(points = cbind(X, Y),
x_grpB, y_grpB, rad_grpB + 115)
grp <- ifelse(grpA, ifelse(grpB, 'D', 'A'),
ifelse(grpB, 'B', 'C'))
} )
```

For the few rows you gave us, all are in group C.

Source (Stackoverflow)

Comments