I look for an efficient way to work with a column as a part of large dataset. Unfortunatly because of my limited experience, so far the only sure way for me was to extract the column first, do my replacements and then insert the column back (e.g. like here Elegant operations on matrix rows and columns)
I wonder what would be a more direct way for working with a column as a part of the whole dataset (my dataset is a list of lists, not "Dataset", as as otherwise I could have used this technique Replace Elements in a Dataset by rules)
Here is an example of my data:
data={{"age", "type", "size"},{10,"cat",1},{22,"dog",5},{2,"cat",11}};
I would like to replace all "cat" in column "type" by 1 and "dog" by zero. However my question is more general -- just how to work and apply whichever command applicable to a list to a column without extracting it and so obtaining the modification directly within the whole dataset.
MapAt[f,data,{All,2}]
, wheref
can be defined to make desired transformations, sayf["cat"]:=1; f["dog"]:=0; f[x_]:=x
. $\endgroup$data[[All,2]]=data[[All,2]]/.{"cat":> 1, "dog":> 0};data
? $\endgroup$MapAt[f,data,{All,2}];data
. If the dataset is large you may (or may not) want that. All is explained in the 'Elegant operations...' link you quote. $\endgroup$