Merging two categorical columns using a rule
    6 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Wendy Cameron
 am 11 Mai 2018
  
    
    
    
    
    Kommentiert: Wendy Cameron
 am 12 Mai 2018
            Hi,
I am wanting to merge/add 2 categorical columns but need to use a rule.
For example a= [Flowering not not not], b=[not not not budburst] and I want the final output to be c=[Flowering not not Budburst]?
Basically, when merging/adding the two vectors, I want the word "flowering" or "budburst" to take precedence over "not". "Flowering" will never clash with "budburst" and "not "not" would give a "not".
Thanks, Wendy
0 Kommentare
Akzeptierte Antwort
  Ameer Hamza
      
      
 am 11 Mai 2018
        
      Bearbeitet: Ameer Hamza
      
      
 am 11 Mai 2018
  
      Here is one way
% Extracting data from yor file
data = readtable('phenology.xls');
a = categorical(data.A);
b = categorical(data.B);
% preparing default output vector
c = categorical(repmat({'not'}, size(a)));
indA = ~ismember(a, 'not');
indB = ~ismember(b, 'not');
c(indA) = a(indA);
c(indB) = b(indB);
isequal(c, data.C)
ans =
  1 <--- The generated c is same as in your xls file.
0 Kommentare
Weitere Antworten (3)
  Greg
      
 am 11 Mai 2018
        Make your categorical array ordinal (with "not" as the lowest value), then use c = max(a,b). This is only guaranteed to work since you said valid values won't conflict.
3 Kommentare
  Greg
      
 am 12 Mai 2018
				Basically, you specify the valueset (allowable entries) in order of smallest to largest:
valueset = {'not';'Budburst';'Flowering'};
var = categorical(a,valueset,'ordinal');
  Wendy Cameron
 am 11 Mai 2018
        1 Kommentar
  Ameer Hamza
      
      
 am 11 Mai 2018
				I just tested my code with your data and it is giving correct output. Can you tell which line is creating the error? Refer to my edited answer to see how to read data from the xls file.
Siehe auch
Kategorien
				Mehr zu Tables finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


