How to replace string values in a column of a table into numeric?

85 ビュー (過去 30 日間)
Vaishali Ravi
Vaishali Ravi 2020 年 8 月 3 日
編集済み: Hamidreza 2022 年 9 月 21 日
I have a table X
It has a column named 'fruit' with string values such as {'apple','orange','grapes'}, There are 13 different string values in the column 'fruit'
Now I need to change it as if apple or orange then 1, grapes then 2 and so on.
How can I do that?
  4 件のコメント
Walter Roberson
Walter Roberson 2020 年 8 月 3 日
you need to replace the entire table variable if you are changing datatype.
It is often easier to add a new variable with the derived data and either leave the old data or delete the variable.
By the way, use ismember and the second output to find the index of the string. Use the index of the string to index into a vector that maps to numeric values, the thus allowing you to map orange and apple to the same number.
This can be done in a small number of vectorized lines m
Vaishali Ravi
Vaishali Ravi 2020 年 8 月 3 日
can you giva a sample code on how to use ismember?

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2020 年 8 月 3 日
fruit_types = {'apple', 'orange', 'grape', 'banana', 'kumquat', 'peach', 'tomato', 'strawberry', 'pineapple', 'kiwi', 'blueberry', 'durian', 'avacodo'};
fruit_value = [ 1, 1, 2, -4, 5.3, 1 3 5.3, 11, 2, -5.3, 1, 4];
[wasfound, idx] = ismember(X.fruit, fruit_types);
f_values = nan(length(idx), 1));
f_values(wasfound) = fruit_value(idx(wasfound));
X.fruit_value = f_values;
  1 件のコメント
Hamidreza
Hamidreza 2022 年 9 月 21 日
編集済み: Hamidreza 2022 年 9 月 21 日
When I run your code I get the error message below:
Error using cell/ismember (line 34)
Input A of class double and input B of class cell must be cell arrays of character vectors, unless one is a character vector.
Any advice to resolve it?
Thanks,
Hamid

サインインしてコメントする。

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 8 月 3 日
X.fruit = categorical(X.fruit);
X.fruit = renamecats(X.fruit,{'apple','orange','grapes'},{'1','2','3'});
  4 件のコメント
Vaishali Ravi
Vaishali Ravi 2020 年 8 月 4 日
Actually I tried str2double, it produced Nan values.
Cris LaPierre
Cris LaPierre 2020 年 8 月 4 日
You have to combine all 3 lines of code:
  1. Convert to categorical
  2. Rename categories
  3. Convert categories to double
% set up a table variable with 20 repeating fruit names
fruit = {'apple','orange','grapes'}';
ind = randi(3,20,1);
fruit = fruit(ind);
X = table(fruit)
Here you see the text values for fruit
X =
20×1 table
fruit
__________
{'apple' }
{'apple' }
{'grapes'}
{'grapes'}
{'orange'}
{'grapes'}
{'grapes'}
{'apple' }
{'orange'}
{'orange'}
{'grapes'}
{'apple' }
{'orange'}
{'orange'}
{'orange'}
{'grapes'}
{'apple' }
{'grapes'}
{'orange'}
{'apple' }
Now convert the text to numbers
% Change names to numbers
X.fruit = categorical(X.fruit);
X.fruit = renamecats(X.fruit,{'apple','orange','grapes'},{'1','2','3'});
X.fruit = str2double(string(X.fruit))
Now you see the fruit values are numbers
X =
20×1 table
fruit
_____
1
1
3
3
2
3
3
1
2
2
3
1
2
2
2
3
1
3
2
1
Use summary to confirm fruit is now a double.
>> summary(X)
Variables:
fruit: 20×1 double
Values:
Min 1
Median 2
Max 3

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by