How to convert one column of a table into numeric data type corresponding to the other column of the same table?
    6 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Haider Ali
 am 26 Aug. 2021
  
    
    
    
    
    Beantwortet: Peter Perkins
    
 am 2 Mär. 2022
            I have the table as shown below:

I need to calculate a multiple comparison test, which includes the means plots for each genre. When I try to apply its process, they say that the genre (string) and runtime (int) are of different types so cant be processed. I want to convert the genre column to numeric w.r.t the runtime column. E.g Action = 1, comedy = 2, Animation  = 3 and Biography = 4. Can someone please guide me how to do this? I am stuck at here. Thanks alot in advance,
0 Kommentare
Akzeptierte Antwort
  Wan Ji
      
 am 26 Aug. 2021
        
      Bearbeitet: Wan Ji
      
 am 26 Aug. 2021
  
      If you have a table T with field names genre and runtime, then
genre = {'ab','a','a','o','abo'}';
runtime = [11,12,13,14,45]';
T = table(genre, runtime);
[Q,ia,ic] = unique(T{:,'genre'});
Q = categorical(Q); % this is the categorical of genre
genre = ic;
T(:,'genre') = [];
T{:,'genre'} = ic;
3 Kommentare
  Wan Ji
      
 am 26 Aug. 2021
				
      Bearbeitet: Wan Ji
      
 am 26 Aug. 2021
  
			So I also need your encouragement, please acecept my answer if it worked@Haider Ali
Weitere Antworten (1)
  Peter Perkins
    
 am 2 Mär. 2022
        This is perhaps slightly more clear:
>> genre = {'ab','a','a','o','abo'}';
>> runtime = [11,12,13,14,45]';
>> T = table(genre, runtime);
>> T.genre = categorical(T.genre);
>> T.genreID = double(T.genre)
T =
5×3 table
genre    runtime    genreID
_____    _______    _______
ab        11          2   
a         12          1   
a         13          1   
o         14          4   
abo       45          3   
0 Kommentare
Siehe auch
Kategorien
				Mehr zu Elementary Math 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!


