Changing Double to String Automatically in a Dataset Array

Hi there,
I have a dataset array called 'data' of 2738 observations with 9 columns(2738*9) in size.
The 3rd column header is 'Surf' and contains 1's, 10's, 100's, 1000's, 10000's and 100000's.
I intend to write a for loop that goes through the data.surf column and wherever it sees a 1 it replaces it with 'F' and similarly replaces 10's with 'R' and so forth.
It seems as though I can replace the whole column with a 'F' by just typing in data.surf = 'F' but I am not quite sure how to write a mini script that automates this process. Any help or guidance will be appreciated.
Thanks in advance. Nj

 Risposta accettata

Here are two ways to do it:
%% Method 1, using "containers.Map"
ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf');
C = containers.Map([1 10 100 1000 10000 100000],{'F' 'R' 'A' 'B' 'C' 'D'});
ds.Surf = C.values(num2cell(ds.Surf))
%% Method 2, using ISMEMBER
ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf');
C = {[1 10 100 1000 10000 100000]; {'F'; 'R'; 'A'; 'B'; 'C'; 'D'}};
[~,loc] = ismember(ds.Surf,C{1});
ds.Surf = C{2}(loc)

6 Commenti

Thank you Teja Muppirala. I used the containers.Map method and it worked perfect!!! I really appreciate you helping me out here..and I would like to thank Walter as well as Sean for helping me out. Have a wonderful new year. Cheers.
[tf, loc] = ismember(surfcodes, 'FRSXHU');
codevals = [1;10;100;1000;10000;100000];
surfs = codevals(loc);
One last question however, is that if I chose to do vice versa (i.e replace string 'F' 'R' etc with Numbers 1, 10 etc) I could not simply change the order of the appearance in the containers.Map method can I? Because when I create a new container and try to replace the corresponding string with numbers I keep getting the error: ??? Error using ==> containers.Map Specified value type does not match the type expected for this container. When infact the help on containers.Map suggests that the keys can be Characters and Values can be any data type. Do you know how this can be solved?
You need to use cell array of strings to specify the keys. e.g.,
dataset({'F', 'R', 'S', 'X', 'H', 'U'}, ...)
If you need to do be able to reverse the operation also, then I think the ISMEMBER way might be simpler. You could still do it either way though:
%% Using containers.Map
ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf');
C = containers.Map([1 10 100 1000 10000 100000],{'F' 'R' 'A' 'B' 'C' 'D'});
ds.Surf = C.values(num2cell(ds.Surf))
Cinv = containers.Map({'F' 'R' 'A' 'B' 'C' 'D'},[1 10 100 1000 10000 100000]);
ds.Surf = cell2mat(Cinv.values(ds.Surf))
%% Using ISMEMBER with an appropriate cell array
ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf');
C = {[1; 10; 100; 1000; 10000; 100000]; {'F'; 'R'; 'A'; 'B'; 'C'; 'D'}};
[~,loc] = ismember(ds.Surf,C{1});
ds.Surf = C{2}(loc)
[~,loc] = ismember(ds.Surf,C{2});
ds.Surf = C{1}(loc)
Thank you so much Teja Muppirala. I really appreciate your answers, they helped me out a lot!

Accedi per commentare.

Più risposte (3)

So something like this?
ds = dataset(rand(10,1)>0.5,'VarNames','Surf');
idx = ds.Surf;
ds.Surf = repmat('F',numel(idx),1);
ds.Surf(idx) = 'T';
ds.Surf

4 Commenti

very close, but I encounter an issue with the second line of the code. As in: idx=ds.Surf; automatically changes all the contents in the Surf column to the string 'F' according to your code.
I am hoping to be able to go through each row in the column 'Surf' and when the number 1 appears I would like to replace it with a string 'F' and likewise the number 10 to a string 'R' and so forth. Every possible combination I have tried says something like 'cannot change a double to a string' or something to that effect. I am hoping there is a straightforward solution to this.
Thank you so much for looking into this. Nj
I suspect you cannot have both string and double in a single column, so you would not be able to handle this incrementally. Instead you would need to create a new array for the strings and fill that in, and then set the field to that new array. It is possible that the dataset would complain about the change in datatype at the end, but at least you would not be trying to store both types at the same time.
I think you are absolutely right. Matlab probably does not allow for me to incrementally change from a double to a string. I might have to create a new column and have matlab input string values to the corresponding double values. Which leads me to my question, How can I write a for loop to index each individual cell in the column 'Surf' and simultaneously write the corresponding 'string' value in the new column? I guess my issue is with the for loop indexing in a dataset array. Thank you so much for taking the time to look into this and respond to me. I really appreciate it. Nj
If you are looking at data.surfs(K) then the new thing to write would be something like data.surfcodes(K)

Accedi per commentare.

letteridx = 1 + round(log10(data.surf);
letters = 'FRXUH';
data.surf = letters(letteridx);

2 Commenti

Unfortunately when I try running this code, it keeps telling me that the index exceeds the matrix dimensions. I am hoping there is another way of indexing each element in the "Surf" column and changing the numbers (1,10,100) one by one to a strings (f, R and S) respectively?
Thank you so much for looking into this for me. Nj
Could you show max(data.surf) before this code?
I notice that I should have allowed one more letter,
letters = 'fRSXUH';
Change the XUH to the appropriate codes for 1000, 10000, and 100000

Accedi per commentare.

Nj, you might also consider using an ordinal variable here:
>> ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf')
ds =
Surf
10
100
10
1000
1e+05
1
1
10000
>> ds.Surf = ordinal(ds.Surf,{'F' 'R' 'A' 'B' 'C' 'D'},[1 10 100 1000 10000 100000])
ds =
Surf
R
A
R
B
D
F
F
C
You can now do things like
>> ds(ds.Surf < 'A',:)
ans =
Surf
R
R
F
F
where Surf is created so that F<R<A<B<C<D (which may or my not be what you want).

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by