How do i do calculations within a table based on groups

2 次查看(过去 30 天)
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 个评论
dpb
dpb 2019-9-8
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.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2019-9-9
编辑:Cris LaPierre 2019-9-9
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Preprocessing 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by