How to apply a function to a column conditional on another slot's value?
The Dataset documentation show only how to apply a function to named slots independently. The {key-> foo}
operator can only access values associated to key
Given input dataset:
data = <|"data1" -> <|"PLATFORM" -> "GoogleDrive", "LOCATION" -> "path1"|>,
"data2" -> <|"PLATFORM" -> "Dropbox", "LOCATION" -> "path2"|>,
"data3" -> <|"PLATFORM" -> "GoogleDrive", "LOCATION" -> "path3"|>|> //Dataset;
Want to apply foo
to "LOCATION"
if "PLATFORM"=="Dropbox"
is True
, yielding the modified data:
This workaround solution using GroupBy
and Merge
is ugly, rearranges data's rows (undesirable), and forces the user to define Identity functions:
platformFunctions = <| "GoogleDrive" -> Identity, "Dropbox" -> foo|> ;
{data [GroupBy[Key@"PLATFORM"]] // Normal, platformFunctions} //
Merge[Normal[
Dataset[First[#]][All, {"LOCATION" -> Last[#]}]] &] //
Values // Normal // Flatten // Association // Dataset
Is there more convenient method?