Instantly share code, notes, and snippets.

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))
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
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
---
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.
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