Skip to content

Instantly share code, notes, and snippets.

@AndrewLJackson
Created July 17, 2017 10:06
Show Gist options
  • Save AndrewLJackson/af7533e17ee9a85ac057cb0f27d3a8bd to your computer and use it in GitHub Desktop.
Save AndrewLJackson/af7533e17ee9a85ac057cb0f27d3a8bd to your computer and use it in GitHub Desktop.
Demonstration of how to split a data frame into a list when the grouping variable is a character string
library(dplyr)
# use the iris dataset
dd <- iris
# The normal way to group or split is by directly referencing the var object
dd %>% group_by(Species)
dd %>% split(.$Species)
# If using this with a user defined reference to the variable such as
# when doing this within a function, it is not obvious how to construct the
# .$variable call properly as I would normally do this as a string.
# specify a character string reference to the variable to group by
g <- "Species"
# one can group with character strings
dd %>% group_by(.dots = g)
# Trying this with split fails
dd %>% split(.dots = g)
# or worse returns a list of length 1 that can then get passed on without
# one realising
dd %>% split(g)
# To emulate the splitting, we need the following which I found at
# https://stackoverflow.com/questions/39638233/grouped-data-frame-to-list
dd %>% split(., .[,g])
# Its not pretty, and doesnt match the dplyr syntax but it works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment