Esty
Esty

Reputation: 41

subtract a different vector from each row in a matrix in r

I'm using R. These are my data:

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

Answers (2)

Ronak Shah
Ronak Shah

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

akrun
akrun

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

Related Questions