I kept getting my data mixed up so I decided to see if I could construct a package adding a R DataFrame like construct to Mathematica. I managed to cobble something together that sort of works, but I've run into some snags. I decided to use replacement rules as the basic data structure for the dataframe. I was really the only solution I could think of that wouldn't be too convoluted to construct.
What I have thus far package-wise is this:
BeginPackage["DataFrame`"]
CreateDataFrame::usage="CreateDataFrame[data] creates a data frame out of the data table. It assumes the first row is the header."
FrameNames::usage="Names[dataframe] gives the header names of the dataframe."
Begin["`Private`"]
CreateDataFrame[data_]:=
Dispatch@Table[
Rule[i,Rest@First@Select[Transpose@data,First@#==i&]],
{i,First@data}
]
FrameNames[dataFrame_Dispatch]:=Table[dataFrame[[1,i,1]],{i,Length@dataFrame[[1]]}]
End[]
EndPackage[]
I also tried to created some custom notation to access parts of the dataframe but I haven't yet figured out how to get it to work when using a package:
Needs["Notation`"]
Notation[ParsedBoxWrapper[
RowBox[{"x_Dispatch", " ", "$", " ",
"y_String"}]] \[DoubleLongRightArrow] ParsedBoxWrapper[
RowBox[{"ReplaceAll", "[",
RowBox[{"y_String", ",", "x_Dispatch"}], "]"}]]]
So I have a number of problems I was hoping someone here could help me with:
How would I go about defining the custom notation from within the package. Currently I have to define it in the notebook to get it to work?
Could I somehow give the dataframe a custom head of DataFrame
, preferably without breaking anything?
Is there any way to make the formatting of the output of CreateDataFrame
nicer?
While the FrameNames
function works as it is I feel it would be more Mathematica-like if it were possible to access it like this instead: dataframe["Names"]
, but I'm not sure how to make it so.
The custom notation I defined with $
sort of works it is still a bit awkward. To use it I have to make sure I manually include the space between that dataframe name and the $
, as well use quotation marks for the variable name. Any ideas for streamlining it more would be wonderful.
Then there is the performance side. The performance isn't terrible, but it is not great either, especially not when creating the dataframe. Any possible recommendations for improvements here would always be appreciated.
P.S. I realise I am piling on a bunch of questions here but I suspect there is a lot of overlap in the answers to them and so splitting the questions up into separate posts would not be helpful.