I have a bunch of means associated with unique values. The unique values are lines that I tested on two different substrates. in R I want to build a loop that will take the unique value and divide the means (pheno_output) of substrate A by substrate B. and spit this into a new vector.
This is what I am working on.
tmpmeans<- rep(NA, 2);
for (i in unique(SplitnonMEANs$Line)){
tmpmeans<- SplitnonMEANs$pheno_output[i]/SplitnonMEANs$pheno_output[i]
}
example data:
s;
Line phenotype FoodSource pheno_output
1 non A 123
2 non A 456
1 non B 789
2 non B 159
Can anyone help me do this?
If you only have two values per group, you can use any of these
divide <- function(x) {
stopifnot(length(x) == 2L)
x[1L]/x[2L]
}
aggregate(pheno_output ~ Line, df, divide)
# Line pheno_output
# 1 1 0.1558935
# 2 2 2.8679245
with(df, tapply(pheno_output, Line, divide))
# 1 2
# 0.1558935 2.8679245
sapply(split(df$pheno_output, df$Line), divide)
# 1 2
# 0.1558935 2.8679245