Calculation of relative standard deviation with a custom function in R
I have measured concentrations of elements in a number of samples. Each concentration is an average of three measurements. Also the standard deviation of these measurements is recorded. I tried to calculate the relative standard deviation with a custom function in R but something is wrong here...
library(tidyverse)
data - tribble(
~Sample, ~Cu_conc, ~Fe_conc, ~K_conc, ~Mg_conc, ~Mn_conc, ~Cu_std, ~Fe_std, ~K_std, ~Mg_std, ~Mn_std,
A, 104.126, 0.729185, 283.741, 21.348, 440.639, 0.783757, 0.00637, 1.544, 0.100056, 4.586,
B, 32.409, 0.782756, 451.802, 43.196, 727.316, 0.774423, 0.014793, 0.473762, 0.007142, 0.231931,
C, 73.447, 1.959, 243.566, 15.113, 201.526, 0.856306, 0.082993, 1.428, 0.175292, 2.529,
D, 125.114, 1.5, 273.146, 34.369, 328.96, 1.429, 0.010748, 0.109602, 0.112713, 2.553,
E, 212.173, 3.045, 163.773, 24.257, 1448.46, 1.302, 0.015061, 0.729027, 0.153371, 6.866,
F, 185.085, 2.776, 176.943, 24.902, 1254.8, 0.0915, 0.062706, 0.252296, 0.009758, 2.233,
G, 40.643, 1.192, 87.437, 10.387, 299.003, 0.419244, 0.004575, 0.349594, 0.035921, 0.28355,
H, 38.938, 1.014, 84.263, 10.651, 150.795, 0.210011, 0.005417, 0.540937, 0.003111, 2.863,
I, 35.066, 0.6763, 153.529, 11.861, 405.314, 0.706683, 0.011766, 0.110662, 0.059892, 3.1,
J, 54.571, 1.152, 91.632, 15.625, 213.258, 0.161998, 0.001985, 0.490803, 0.003677, 0.297692
)
# analysed elements
elements - c(Cu, Fe, K, Mg, Mn)
# function: relative standard deviation
relstdev - function(conc, stdev) {
ifelse(is.na(conc) | is.na(stdev) | conc == 0, NA,
ifelse(stdev = conc, 100,
abs(100 * stdev / conc)
)
)
}
# calculation
for (e in elements) {
data - data %% mutate({e}_rsd := relstdev({e}_conc, {e}_stdev))
}
When the program is run I get new columns for each element with rsd but the values are all 100: not what I expected!
data %% select(ends_with(_rsd))
# A tibble: 10 x 5
Cu_rsd Fe_rsd K_rsd Mg_rsd Mn_rsd
dbl dbl dbl dbl dbl
1 100 100 100 100 100
2 100 100 100 100 100
3 100 100 100 100 100
4 100 100 100 100 100
5 100 100 100 100 100
6 100 100 100 100 100
7 100 100 100 100 100
8 100 100 100 100 100
9 100 100 100 100 100
10 100 100 100 100 100
This implies that in the rsd-function the condition stdev = conc had to be true. This is not the case:
data$Cu_std = data$Cu_conc
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Can someone help me please?
Topic programming statistics r
Category Data Science