Skip to content

Instantly share code, notes, and snippets.

View ifknot's full-sized avatar
💭
Get off my lawn!

ifknot ifknot

💭
Get off my lawn!
View GitHub Profile
@ifknot
ifknot / read_csv.h
Created October 17, 2020 15:47
read csv data
#pragma once
#include "data_frame.h"
namespace R {
// TODO: column names and classes
data_frame read_csv(std::string file_path, bool has_header = true);
}
@ifknot
ifknot / r_date.cpp
Created October 17, 2020 15:44
r-ish date type stream ops
#include "r_date.h"
#include <iomanip>
#include <cassert>
#include <algorithm>
std::ostream& operator<<(std::ostream& os, const R::r_date& date) {
os << std::put_time(&date.tm, date.format.c_str());
return os;
@ifknot
ifknot / r_date.h
Last active October 17, 2020 15:44
r-ish date type
#pragma once
#include <string>
#include <ctime>
#include <ostream>
namespace R {
/**
* An R-ish calender date type
@ifknot
ifknot / tokenize.h
Created October 17, 2020 13:17
tokenize header
#pragma once
#include <string>
namespace R {
/**
* The 7 R-ish data types:
* r_raw, r_integer, r_numeric, r_string, r_logical, r_complex, r_date
* converted to tokens with an extra 'broken' token for unrecognized type
@ifknot
ifknot / mpg.txt
Created October 17, 2020 12:48
output from mpg.cpp
read dumb data into heterogeneous container
manufacturer model hwy displ year cyl drv cty trans fl class
(str) (str) (int) (num) (int) (int) (str) (int) (str) (str) (str)
0 audi a4 29 1.8 1999 4 f 18 auto(l5) p compact
1 audi a4 29 1.8 1999 4 f 21 manual(m5) p compact
2 audi a4 31 2 2008 4 f 20 manual(m6) p compact
3 audi a4 30 2 2008 4 f 21 auto(av) p compact
4 audi a4 26 2.8 1999 6 f 16 auto(l5) p compact
5 audi a4 26 2.8 1999 6 f 18 manual(m5) p compact
@ifknot
ifknot / mpg.cpp
Created October 17, 2020 12:44
Read dumb data into C++ heterogeneous data frame
#include <iostream>
#include "data_frame.h"
#include "read_csv.h"
#include "head.h"
using namespace R;
int main() {
@ifknot
ifknot / mpg.R
Created October 17, 2020 12:33
R code to load and show first 6 entries of the mpg.csv data set
# Import the data and look at the first six rows
car_data <- read.csv(file = 'data/mpg.csv')
head(car_data)
manufacturer model hwy displ year cyl drv cty trans fl class
(str) (str) (int) (num) (date) (int) (str) (int) (str) (str) (str)
1 audi a4 29 1.8 1999 4 f 18 auto(l5) p compact
2 audi a4 29 1.8 1999 4 f 21 manual(m5) p compact
3 audi a4 31 2 2008 4 f 20 manual(m6) p compact
4 audi a4 30 2 2008 4 f 21 auto(av) p compact
#include "r_data_frame.h"
namespace R {
variant_vector as_dates(std::vector<std::string> source) {
R::variant_vector tm_dates;
for (const auto& date : source) {
std::stringstream ss;
ss << date + "T00:00:00Z ";
std:tm tm;
heterogeneous container
id name salary start_date
0 1 Rick 623.3 2012-01-01
1 2 Dan 515.2 2013-09-23
2 3 Michelle 611 2014-11-15
3 4 Ryan 729 2014-05-11
4 5 Gary 843.25 2015-03-27
Dan earns $515.2
#include <iostream>
#include "r_data_frame.h"
int main() {
std::cout << "heterogeneous container\n\n";
R::data_frame d;