Skip to content

Instantly share code, notes, and snippets.

View jonocarroll's full-sized avatar
👨‍💻
Learning all the languages

Jonathan Carroll jonocarroll

👨‍💻
Learning all the languages
View GitHub Profile
@jonocarroll
jonocarroll / 8queens.R
Last active April 11, 2018 13:44
8-Queens Tidyverse Solution
#### 8-QUEENS TIDYVERSE SOLVE ####
library(tidyverse)
## number of queens (and size of boards)
n <- 8
## if we brute-foced this from the start we would be searching
choose(n^2, n) # n=8: 4426165368
## solutions which is just too many. We can simplify this by
@jonocarroll
jonocarroll / matrix.as.data.frame.R
Created April 25, 2018 00:17
Storing a matrix in a single data.frame column
## A data.frame can contain a complex structure
## under certain conditions.
## create a normal matrix but give it a new class
set.seed(1)
mymat <- structure(
matrix(sample(1:5, 40, replace = TRUE), 10, 4),
class = "actually_a_matrix"
)
mymat
x <- letters
numbers <- runif(8)
numbers
[1] 0.3229318 0.5933054 0.7778408 0.3898947 0.1309717 0.7501378 0.3206379 0.3379005
@jonocarroll
jonocarroll / datasauRus_gganimate.R
Created August 15, 2018 06:20
Animated datasauRus
# modified from https://gist.github.com/sa-lee/c271b47d8065412d3d42c1ad8e63048d
library(datasauRus)
library(gganimate)
library(magrittr)
p <- datasaurus_dozen %>%
ggplot() +
geom_point(aes(x = x, y = y), col = "steelblue") +
theme(legend.position = 'none',
aspect.ratio = 1,
@jonocarroll
jonocarroll / plus_character.R
Last active October 4, 2018 07:52
Python/Javascript String Concatenation in R
`+` <- function(e1, e2) {
## unary
if (missing(e2)) return(e1)
if (!is.na(suppressWarnings(as.numeric(e1))) & !is.na(suppressWarnings(as.numeric(e2)))) {
## both arguments numeric-like but characters
return(base::`+`(as.numeric(e1), as.numeric(e2)))
} else if ((is.character(e1) & is.na(suppressWarnings(as.numeric(e1)))) |
(is.character(e2) & is.na(suppressWarnings(as.numeric(e2))))) {
## at least one true character
return(paste0(e1, e2))