I currently have a dataset, and I would like to add a column based on another list.
For example:
ds = Dataset[{<|"name" -> "bob"|>, <|"name" -> "joe"|>}];
ages = {1,2};
I want to get
Dataset[{<|"name" -> "bob", "age"->1|>, <|"name" -> "joe", "age"->2|>}];
I was thinking of using MapIndex, but I don't really know how to make reform the results into a dataset again, and it seems wasteful.
How can I do this?
Answer
ds[MapIndexed[Append[#1, "age" -> ages[[First@#2]]] &]]
{<|"name" -> "bob", "age" -> 1|>, <|"name" -> "joe", "age" -> 2|>}
Note 10.0.2 throws a warning - who knows what's going on in that private type system:
First::normal: Nonatomic expression expected at position 1 in \
First[TypeSystem`ZSignatures`PackagePrivate`i]
Comments
Post a Comment