Add a column to some Dataset

Hello everybody!

It’s likely a silly question, but I can’t find the way to add a column to some dataset.
Consider:

dataset = Dataset[{
   <|"a" -> 1, "b" -> 3, "c" -> {1}|>,
   <|"a" -> 2, "b" -> 8, "c" -> {2, 3}|>,
   <|"a" -> 3, "b" -> 0, "c" -> {3}|>,
   <|"a" -> 4, "b" -> 1, "c" -> {4, 5}|>,
   <|"a" -> 5, "b" -> 12, "c" -> {5, 6, 7}|>,
   <|"a" -> 6, "b" -> -3, "c" -> {}|>}]

and compute

Prov = dataset[All, {#a - #b &}]

How could I build another dataset with Prov as a supplementary column, named “Prov” ,for instance?

Thanks

Hi Claude,

See if that’s what you want to do:

dataset2 = {<|"a" -> 1, "b" -> 3, "c" -> {1}|>, <|"a" -> 2, "b" -> 8, 
    "c" -> {2, 3}|>, <|"a" -> 3, "b" -> 0, "c" -> {3}|>, <|"a" -> 4, 
    "b" -> 1, "c" -> {4, 5}|>, <|"a" -> 5, "b" -> 12, 
    "c" -> {5, 6, 7}|>, <|"a" -> 6, "b" -> -3, "c" -> {}|>};
Prov2 = Table[
   dataset2[[x, 1]] - dataset2[[x, 2]], {x, 1, Length@dataset2}];

Dataset@Join[{Join[Keys@dataset2[[1]], {"Prov"}]}, 
  Table[Join[(Keys@dataset2[[1]] /. dataset2)[[y]], {Prov2[[y]]}], {y,
     1, Length@Prov2}]]

Maybe there is another way to do it, but you can do it this way…

Thank, Claudio!

It works on this small data set, but not on the real one, unfortunately…
It’s a data set about fishing (joined).

We need first routines to estimate the second parameter (probability of success) of a negative binomial (or Pascal, in my case) distribution:

ClearAll["Global`*"];

Place = NotebookDirectory[];
SetDirectory[Place <> "/Données"];
\[Epsilon] = 10^-6;
Brut = Import["Peches2005.xlsx", {"Dataset", 1}, HeaderLines -> 1];
pEstPonct[temps_, fish_] := 
  If[N@fish <= 1 + \[Epsilon], 
   0, (fish - 1)/(temps + fish - 1)];  (* Haldane; 1945, Sahai; 1979 *)


pEstGroup[temps_, fish_ /; fish >= 1] := 
 Block[{freq, \[Theta], x, fx, p},
  		freq = Tally[temps];
  		x = First@Transpose@freq;
  		fx = Last@Transpose@freq;
  		\[Theta] = (x.fx)/((x.fx) + fish Total@fx - 1);
  		p = (1 - \[Theta]);
  N[p]	
  ]

Then, the sub-dataset associated with trouts (coded “TRF”) is extracted:

Cherche = "TRF";
Lepoisson = Brut[Select[#"nom_sp" == Cherche &]];

Now, estimation of the probability (2 methods); Diff denotes the time spent, nb (the variable “Total” , an unfortunate coding) the number of fishes; Pas is the time unit.

Pas = 4;     (* Unité de temps, en minutes : temps entre deux actes \
de pêche (lancers,...)  *)
Diff = Flatten@
   Normal@Lepoisson[
     All, {N[(AbsoluteTime@#"hcont" - 
           AbsoluteTime@#"hdeb") (1/(60 Pas))] &}];
nb = Lepoisson[All, {"Total"}];
Paire = Dataset@
   Join[{Join[Keys@nb[[1]], {"Diff"}]}, 
    Table[Join[(Keys@nb[[1]] /. nb)[[y]], {Diff[[y]]}], {y, 
      Length@Diff}]];
Head@Paire

“Paire” denotes the pair (total, Diff), built with your method. It seems correct but, for some reason, the following instructions don’t work:

Paire = Select[Paire, #Diff > 0 &];
Length@Paire
Paire = Select[Paire, #Total =!= "" &];
Length@Paire

Any idea?

Claude

Peches2005.xlsx (1.24 MB)

Try:

dataset[All, <|#, "Prov" -> #a - #b|> &]

For working with Dataset I found this nice post inspiring.