Skip to content

Instantly share code, notes, and snippets.

View johnmyleswhite's full-sized avatar

John Myles White johnmyleswhite

View GitHub Profile
@johnmyleswhite
johnmyleswhite / gist:4195980
Created December 3, 2012 16:10
Imitating plyr and reshape in Julia
# A top priority for making DataFrames useful in Julia is the development of
# good documentation and a nice API for doing plyr+reshape style operations
# in Julia. This Gist is a draft of such documentation.
load("DataFrames")
using DataFrames
load("RDatasets")
baseball = RDatasets.data("plyr", "baseball")
@johnmyleswhite
johnmyleswhite / lift.jl
Created October 31, 2020 15:48
wip_lift.jl
import MacroTools: postwalk
function missing_check(es)
if length(es) == 0
false
elseif length(es) == 1
Expr(:call, :ismissing, es[1])
elseif length(es) == 2
Expr(
:call,
struct ExpressionThunk
source::Any
thunk::Any
end
macro thunk(e)
quote
ExpressionThunk(
$(QuoteNode(e)),
() -> $(esc(e))
x = [1, 2]
y = [3, 4]
z = [x, y]
z′ = copy(z)
z′[1] === x
import DataFrames: DataFrame
import Plots
r = range(0.0, 1.0, length=512)
p = Plots.plot(
r,
x -> x^2 * (1 - x)^2,
)
Plots.png(p, "output2.png")
#!/usr/bin/Rscript
png("output.png")
curve(x^2 * (1 - x)^2, from = 0, to = 1)
dev.off()
# time Rscript example.R
# null device
# 1
#
import RCall: @R_str
function runtimes(n_reps)
times = Array{Float64}(undef, n_reps)
x = rand(10_000_000)
for i in 1:n_reps
times[i] = @elapsed sum(x)
end
times
end
> library("stringr")
> str_sub("ñ", start = -1)
[1] "̃"
> str_sub("ñ", start = -1)
[1] "ñ"
function prob_f64(n)
s = 0
for _ in 1:n
z_i = rand(UInt)
x_i = reinterpret(Float64, z_i)
s += Int(x_i == x_i + 1.0)
end
s, n
end
function prob(n)
s = 0
for _ in 1:n
x_i = rand(UInt)
s += Int(Float64(x_i) === Float64(x_i + 1))
end
s, n
end
prob(1_000_000)