How do i do calculations within a table based on groups

2 views (last 30 days)
Hi everyone,
so I have a table which basically has data from a qPCR ( method to measure gene expression).
I have data like this:
I now want to caluculate the deltadeltaCt value which is:
deltadeltaCt = deltaCt value - deltaCtvalue(Ctr)
Every Genotype has its own control. This corresponds to the deltaCt value of the condition 'Ctr'.
For example i would want to calculate:
8,3817 - 8,3817
7,5029 - 8,3817
7,7519 - 8,3817
and in this case ofter the third calculation the value of the deltaCt(control) has to change because it is a different Genotype.
My guess was to use groups and splitapply but so far, I just coudn't get it to run as I want.
I hope you understand what i mean and one of you can probably help.
Thanks and best regards
Julian
  2 Comments
dpb
dpb on 8 Sep 2019
As madhan suggests, having the data to use to illustrate makes things much simpler for those here...showing what code you used specifically and what the result was and what was different than what you expected also would be key--we don't know what "your guess" entailed.
I would strongly suggest you turn the various cellstr() type variables in the table into categorical ones--things will work more cleanly/efficiently that way with the grouping variables.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 9 Sep 2019
Edited: Cris LaPierre on 9 Sep 2019
Doable, but it's not pretty. Groupsummary only allows you to use data from a single variable. I couldn't find a way to get that to work. Splitapply allows a little more flexibility, but only allows a single output per group. That means outputting a cell array will all the values and doing some post processing on it.
% create sample data set
Genotype = categorical(["WT" "WT" "WT" "CD59" "CD59" "CD59" "C6" "C6" "C6" "C6" "C6"]');
condition = ["ctr" "IL1beta" "CTSD" "ctr" "IL1beta" "CTSD" "ctr" "IL1beta" "CTSD" "IL1beta" "CTSD"]';
deltaCt = [8.3817; 7.5029; 7.7519; 7.4766; 7.9030; 8.2387; 7.4396; 7.4460; 7.6484; 7.1357; 7.5693];
tbl = table(Genotype,condition,deltaCt)
% Identify which row in the group contains the control value
% Subtract that value from all other values in the group. Output result as {cell array}
func = @(x,y) {y - y(x=="ctr")};
G = findgroups(tbl.Genotype);
corrected = splitapply(func,tbl(:,["condition","deltaCt"]),G);
% reshape output to contain a single value per row
corrected = vertcat(corrected{:});
% Rearrange rows to (hopefully) align with original table
[~,I]=sort(G)
tbl.deltadeltaCt(I) = corrected
I haven't tested this exhaustively. The trick is going to be making sure the rearranged rows always align with the orginal data set.

More Answers (0)

Categories

Find more on Data Preprocessing in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!