필터 지우기
필터 지우기

How do i do calculations within a table based on groups

조회 수: 5 (최근 30일)
Julian
Julian 2019년 9월 8일
편집: Cris LaPierre 2019년 9월 9일
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
madhan ravi
madhan ravi 2019년 9월 8일
Attach the table as .mat file.
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에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by