Skip to content

Instantly share code, notes, and snippets.

Matt Pettis mpettis

  • Personal
  • Minneapolis, MN
Block or report user

Report or block mpettis

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mpettis
mpettis / spread-group-percent.Rmd
Last active Mar 22, 2019
dplyr spread group percent
View spread-group-percent.Rmd
---
title: "spread-group-percent"
author: "Matt Pettis"
date: "March 22, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
@mpettis
mpettis / geosphere-haversine-example.R
Last active Feb 4, 2019
Using geosphere/haversine matrix function with dplyr mutate operations
View geosphere-haversine-example.R
library(geosphere)
library(tidyverse)
# Simple dataframe and use case:
ref_ll <- c(-95.0, 45.0)
df__ <- tribble(
~lon, ~lat,
-95.0, 45.0,
-96.0, 45.0,
View run-length-encoding-to-dataframe.R
################################################################################
# Make a function that does what I want
# Take a dataframe, identify the target column with runs, ID the index column,
# which is the column that I want to capture the first and last values of for
# runs.
#' rle_as_df
#'
#' Turn run-length encoding into a dataframe so it can be used with ggplot/geom_area.
View R-NULL-as-list-values-processing.R
# Dealing with NULLs as list values
library(tidyverse)
# As advertised on the tin:
is.null(NULL)
#> [1] TRUE
# is.null() is not vectorized?
is.null(list(NULL))
@mpettis
mpettis / R-reduce-lists-of-vectors.R
Last active Nov 14, 2018
R: Reduce, functional programing, boolean condition reduction
View R-reduce-lists-of-vectors.R
``` r
## 'or'ing together same-length vectors of booleans.
## use case is to set up sets of booleans that have the same length and should
## be all 'or'ed together to form a condition.
# Base R way:
lstt <- list(
a = c(T, F, F, F)
, b = c(F, T, F, F)
, c = c(F, F, T, F)
View r-json-and-set-list-elements-to-null.R
library(tidyverse)
library(jsonlite)
#>
#> Attaching package: 'jsonlite'
#> The following object is masked from 'package:purrr':
#>
#> flatten
# Ref: https://stackoverflow.com/questions/7944809/assigning-null-to-a-list-element-in-r
@mpettis
mpettis / r-purrr-named-lists-of-dataframes-to-single-dataframe.R
Last active Apr 23, 2018
r-purrr-named-lists-of-dataframes-to-single-dataframe
View r-purrr-named-lists-of-dataframes-to-single-dataframe.R
# Ref: https://jennybc.github.io/purrr-tutorial/ls02_map-extraction-advanced.html#list_inside_a_data_frame
# Ref: https://github.com/tidyverse/tidyr/issues/22
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(purrr))
# Make iris dataset into list of data frames split by `Species`.
my_list <- split(iris, iris$Species)
str(my_list)
#> List of 3
View R-user-group_tidyr-talk.Rmd
---
title: "R-user-group_tidyr-talk"
author: "Matt Pettis"
date: "November 22, 2017"
always_allow_html: yes
output:
html_document:
toc: true
---
@mpettis
mpettis / gist:baca4c428a8ab499b98b513e7fa3255e
Last active Oct 12, 2017
R: Time Interval Consolidation of Overlaps
View gist:baca4c428a8ab499b98b513e7fa3255e
Very procedural code, but works. If I have a set of time intervals (lubridate intervals), and I want to consolidate any chain of overlapping intervals into a single interval with left end the min of all start times and right side as max of all end times, this is the code to do that.
``` r
## Consolidating intervals
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(lubridate))
# In this table of intervals, rows 3,4, and 5 form a chain of intervals. They should be rolled into 1 interval.
# And note rows 3 and 5 do not themselves overlap, but they are chained together by having overlap with row 4.
@mpettis
mpettis / fourier-fft-periodogram-pick-components.R
Created Aug 11, 2017
Fourier DFT FFT reconstruction from important periodogram contributions
View fourier-fft-periodogram-pick-components.R
## This is just a quick exploration, answering the question:
## How do I reconstruct a signal by picking the highest-power frequency from a periodogram?
## Assumes I keep the constant (DC) term.
## -----------------------------------------------------------------------------
## Fourier decomposition exploration
## -----------------------------------------------------------------------------
x <- 10 * sin(2 * pi * (1:75) / 25) + 300 + rnorm(1:75, 0, 2.5)
x
You can’t perform that action at this time.