34
35:- module(r_data,
36 [ r_data_frame/3, 37 r_data_frame_from_rows/2, 38 r_data_frame_from_dicts/2, 39
40 r_data_frame_to_dicts/2, 41 r_data_frame_to_rows/3, 42
43 r_data_frame_colnames/2, 44 r_data_frame_rownames/2 45 ]). 46:- use_module(r_call). 47:- use_module(library(apply)). 48:- use_module(library(error)). 49:- use_module(library(pairs)). 50:- use_module(library(lists)). 51
52:- meta_predicate
53 r_data_frame(+, +, 0).
77r_data_frame(RVar, ColSpec, Goal) :-
78 must_be(atom, RVar),
79 maplist(arg(1), ColSpec, Names),
80 maplist(arg(2), ColSpec, Vars),
81 Templ =.. [v|Vars],
82 findall(Templ, Goal, Rows),
83 r_data_frame_from_rows(RVar, Rows),
84 colnames(RVar) <- Names.
101r_data_frame_to_dicts(DataFrame, Dicts) :-
102 Cols <- DataFrame,
103 ColNameStrings <- colnames(DataFrame),
104 maplist(atom_string, ColNames, ColNameStrings),
105 pairs_keys_values(Pairs, ColNames, _),
106 dict_pairs(Templ, _, Pairs),
107 maplist(dict_cols(Templ, Dicts), ColNames, Cols).
108
109dict_cols(Templ, Dicts, Name, Col) :-
110 maplist(fill_col(Templ, Name), Col, Dicts).
111
112fill_col(_, Name, Value, Dict) :-
113 nonvar(Dict), !,
114 get_dict(Name, Dict, Value).
115fill_col(Templ, Name, Value, Dict) :-
116 copy_term(Templ, Dict),
117 get_dict(Name, Dict, Value).
132r_data_frame_to_rows(DataFrame, Functor, Rows) :-
133 Cols <- DataFrame,
134 length(Cols, Arity),
135 term_cols(Cols, 1, Arity, Functor, Rows).
136
137term_cols([], _, _, _, _).
138term_cols([Col|Cols], I, Arity, Functor, Rows) :-
139 maplist(term_col(I, Arity, Functor), Col, Rows),
140 I2 is I+1,
141 term_cols(Cols, I2, Arity, Functor, Rows).
142
143term_col(1, Arity, Functor, Value, Term) :- !,
144 functor(Term, Functor, Arity),
145 arg(1, Term, Value).
146term_col(I, _, _, Value, Term) :-
147 arg(I, Term, Value).
157r_data_frame_from_dicts(DataFrame, Rows) :-
158 must_be(atom, DataFrame),
159 must_be(list, Rows),
160 Rows = [Row1|_],
161 dict_keys(Row1, Keys),
162 dict_col_data(Keys, Rows, ColData),
163 compound_name_arguments(Term, 'data.frame', ColData),
164 DataFrame <- Term,
165 colnames(DataFrame) <- Keys.
166
167dict_col_data([], _, []).
168dict_col_data([K|Keys], Rows, [ColI|ColR]) :-
169 maplist(get_dict(K), Rows, ColI),
170 dict_col_data(Keys, Rows, ColR).
177r_data_frame_from_rows(DataFrame, Rows) :-
178 must_be(atom, DataFrame),
179 must_be(list, Rows),
180 Rows = [Row1|_],
181 functor(Row1, _, NCols),
182 col_data(1, NCols, Rows, ColData),
183 append(ColData, [stringsAsFactors = 'FALSE'], ColDataOpts),
184 compound_name_arguments(Term, 'data.frame', ColDataOpts),
185 DataFrame <- Term.
186
187col_data(I, NCols, Rows, [ColI|ColR]) :-
188 I =< NCols, !,
189 maplist(arg(I), Rows, ColI),
190 I2 is I + 1,
191 col_data(I2, NCols, Rows, ColR).
192col_data(_, _, _, []).
198r_data_frame_colnames(DataFrame, ColNames) :-
199 ColNameStrings <- colnames(DataFrame),
200 maplist(atom_string, ColNames, ColNameStrings).
206r_data_frame_rownames(DataFrame, RowNames) :-
207 RowNameStrings <- rownames(DataFrame),
208 maplist(atom_string, RowNames, RowNameStrings)
R data frame handling
This library provides predicates for creating and fetching R data frames. R data frames are typically 2-dimensional arrays where the data is organised in columns. In Prolog, data is typically organised in rows (or records). */