Reputation: 41
I'm using R. These are my data:
Prot_before
: A matrix with 70000 rows and, each row has two columns:
group (There are 700 groups) and value per sample (There are 120
samples).
Prot_healthy
: A matrix with 30000 rows and, each row has two columns:
group (There are 700 groups) and value per sample (There are 45
samples).
For each row in prot_before
, I want to find its value minus the values for all samples in that group in prot_healthy
.
For example:
set.seed(100)
Prot_before <- data.frame(cat = c(rep("aaa", 5), rep("bbb", 5), rep("ccc", 5)), val = runif(15))
Prot_before <- df[order(df$cat, df$val), ]
Prot_before
set.seed(100)
Prot_after <- data.frame(cat = c(rep("aaa", 5), rep("bbb", 5), rep("ccc", 5)), val = runif(15))
Prot_after <- df[order(df$cat, df$val), ]
Prot_after
Now I want the results of each row in prot_before, minus all the samples in the same group aaa
etc, in prot_after
. So for each row in prot_before
I get 45 results.
I tried using sweep but didn't know how the repeat the function for all samples by group.
I'm sorry if this isn't written correctly, I'm not very experienced. Thank you!
Upvotes: 3
Views: 445
Reputation: 388817
We can use apply
row-wise, subset cat
from Prot_after
and subtract all the corresponding val
values
apply(Prot_before, 1, function(x) {
as.numeric(x["val"]) - Prot_after$val[Prot_after$cat == x["cat"]]
})
Upvotes: 1
Reputation: 886978
One option is to split
by 'cat' for each dataset into a list
, and then use outer
to do the subtraction for all the combination rows for corresponding list
elements using Map
Map(outer, MoreArgs = list(FUN = `-`),
split(Prot_before$val, Prot_before$cat), split(Prot_after$val, Prot_after$cat))
Or with sapply
and Map
Map(function(x, y) sapply(x, `-`, y),
split(Prot_before$val, Prot_before$cat), split(Prot_after$val, Prot_after$cat))
Upvotes: 1