Skip to content

Instantly share code, notes, and snippets.

@KobaKhit
Last active August 3, 2018 14:27
Show Gist options
  • Save KobaKhit/f0c1d601f27f9fd154638adc7a669159 to your computer and use it in GitHub Desktop.
Save KobaKhit/f0c1d601f27f9fd154638adc7a669159 to your computer and use it in GitHub Desktop.
Example of how to unnest rows by seat or any other array in a cell.
library(tidyr)
setwd("~/Desktop/unnest")
fname = "file-name.csv"
df = read.csv(paste0(fname,'.csv'), stringsAsFactors = F)
df$seats =
sapply(1:nrow(df), function(x) {
seats = c(df[x,]$first_seat,df[x,]$last_seat)
mi = min(seats)
ma = max(seats)
seq(mi,ma)
})
df = df %>% unnest(seats)
write.csv(df, paste0(fname,'.csv'),row.names = F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment