Reputation: 1038
This answer is out there somewhere, but I'm having a hard time articulate it.
I want to refer to a subset a vector with a dplyr
framework. It's best explained with an example.
I'm trying to calculate something called "Delta E"
Which requires me to refer to one row (called "Control") in this example.
I've tried to find ways to refer to specific values within a column of a data.frame.
In R: subset or dplyr::filter with variable from vector
Here's the example data:
df <- read.table(text="
sampleID L a b
control 54.1 32.5 85.6
A 51.7 34.2 83.3
B 53.7 32.8 85.0
D 53.8 32.7 85.1", header=T)
And I'd like to use dplyr
to get the end result, without doing anything to hacky.
df %>%
mutate(deltaE = sqrt((Lc - L)^2 - (ac - a)^2 + (bc - b)^2)
sampleID L a b deltaE
control 54.1 32.5 85.6 0
A 51.7 34.2 83.3 3.73
B 53.7 32.8 85.0 0.733
D 53.8 32.7 85.1 0.614
Where Lc = 54.1
, ac = 32.5
and bc = 85.6
which correspond to the control sample.
Upvotes: 1
Views: 200
Reputation: 206616
It's not super pretty, but you can do
df %>%
mutate(deltaE = sqrt((L[sampleID=="control"] - L)^2 +
(a[sampleID=="control"] - a)^2 +
(b[sampleID=="control"] - b)^2))
Or split it up into steps
df %>%
mutate(isControl = sampleID=="control",
Lc = L[isControl],
ac = a[isControl],
bc = b[isControl]) %>%
mutate(deltaE = sqrt((Lc - L)^2 +
(ac - a)^2 +
(bc - b)^2)) %>%
select(-isControl, -Lc, -ac, -bc)
Upvotes: 2