How to count the number of non-nan values for combination of multiple variables
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I would like to count the number of non-nan values in the d column for unique combinations of a, b and c (i.e I want to generate the e column in tt). If any a,b or c are NaN then the count should be nan as well
% Initial table
a = [1,1,1,2,2,2,2]';
b = [660, 661, 661, 663, 663, NaN, 663]';
c = [1,2,2,5,5,NaN,6]';
d = [11,12,NaN, 13, 14, NaN,5]';
t = table(a,b,c,d)
% Desired output
a = [1,1,1,2,2,2,2]';
b = [660, 661, 661, 663, 663, NaN, 663]';
c = [1,2,2,5,5,NaN,6]';
d = [11,12,NaN, 13, 14, NaN,5]';
e = [1, 1, 1, 2, 2, NaN, 1]';
tt = table(a,b,c,d,e)
0 commentaires
Réponse acceptée
Voss
le 17 Juil 2024
Modifié(e) : Voss
le 17 Juil 2024
% Initial table
a = [1;1;1;2;2;2;2];
b = [660; 661; 661; 663; 663; NaN; 663];
c = [1;2;2;5;5;NaN;6];
d = [11;12;NaN; 13; 14; NaN;5];
t = table(a,b,c,d)
G = {'a','b','c'};
F = @(x)nnz(~isnan(x));
T = groupsummary(t,G,F,'d');
[~,~,idx] = unique(t(:,G),'rows');
e = T.fun1_d(idx);
e(any(isnan(t{:,G}),2)) = NaN;
tt = addvars(t,e)
Plus de réponses (1)
J. Alex Lee
le 17 Juil 2024
Modifié(e) : J. Alex Lee
le 17 Juil 2024
Not sure what d and e need to be, but i think this should at least partially help
a = [1,1,1,2,2,2,2]';
b = [660, 661, 661, 663, 663, NaN, 663]';
c = [1,2,2,5,5,NaN,6]';
x = [a,b,c]
% any(_,2) goes row-wise ORs.
any(isnan(x),2)
0 commentaires
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!