Message Boards Message Boards

1
|
2370 Views
|
9 Replies
|
9 Total Likes
View groups...
Share
Share this post:

How to add headers to a dataset?

Posted 5 months ago

Suppose I have the following dataset:

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}]

How can I add a header x to the first column and a header y to the second column, so to get the following new dataset:?

Dataset[{
  <|"x" -> 195, "y" -> 2.3|>,
  <|"x" -> 198, "y" -> 6.4|>,
  <|"x" -> 204, "y" -> -3.1|>
  }]

I am also looking for a command to assing units (cm and volts) to the values, so to get the following:

Dataset[{
  <|"x" -> Quantity[195, "Centimeters"], "y" -> Quantity[2.3, "Volts"]|>,
  <|"x" -> Quantity[198, "Centimeters"], "y" -> Quantity[6.4, "Volts"]|>,
  <|"x" -> Quantity[204, "Centimeters"], "y" -> Quantity[-3.1, "Volts"]|>
  }]

Any ideas?

POSTED BY: Ehud Behar
9 Replies

Using a resource function...

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}];

ResourceFunction["DatasetWithHeaders"][ dataset, {"x", "y"}]
POSTED BY: Joshua Schrier

POSTED BY: Vitaliy Kaurov
Posted 5 months ago

Thanks very much.

Regarding this instruction you wrote:

dataset[All,Key["c"]/*<|"ctotal"->Total,"clength"->Length|>] 

Can you show me how to write it in a "FullForm" notatation, With explicit RightComposition? I am not so familiar with this infix shortcut and don't fully understand which expression is the argument of which function.

Another question: You showed how to apply a function (Total and Length) on the values in each row.

Suppose I want to add 3 to each value, or multiply each value by 2, in column a, how to do it?

POSTED BY: Ehud Behar
Posted 5 months ago

For your second question, on adding units:

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}]
Map[{Quantity[#[[1]],"Centimeters"],Quantity[#[[2]],"Volts"]}&,dataset//Normal]//Dataset
POSTED BY: Hans Milton
Posted 5 months ago

Here's one way to start...

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}];
datasetWithHeader = dataset[All, Association @@ Thread[{"x", "y"} -> #] &]

enter image description here

Then you could continue:

datasetWithHeader[All, <|"x" -> Quantity[#x, "Centimeters"], "y" -> Quantity[#y, "Volts"]|> &]

enter image description here

POSTED BY: Eric Rimbey
Posted 5 months ago

Very nice

POSTED BY: Ehud Behar
Posted 5 months ago

Are you required to start with

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}]

or do you have access to the raw data directly? E.g.

data = {{195, 2.3}, {198, 6.4}, {204, -3.1}}
POSTED BY: Eric Rimbey
Posted 5 months ago

I have access to the raw data, and I would like to see an additional approach if I start with "data".

POSTED BY: Ehud Behar
Posted 5 months ago

Then I might do something like this:

Dataset[MapApply[<|"x" -> Quantity[#1, "Centimeters"], "y" -> Quantity[#2, "Volts"]|> &, rawData]]
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract