Skip to content

Instantly share code, notes, and snippets.

@fauxneticien
Created June 16, 2021 05:12
Show Gist options
  • Save fauxneticien/a5e3aff5505fb91f79452537fdfc98e2 to your computer and use it in GitHub Desktop.
Save fauxneticien/a5e3aff5505fb91f79452537fdfc98e2 to your computer and use it in GitHub Desktop.
Transform lexicon from long format to wide format
library(readr)
library(dplyr)
library(tidyr)
library(zoo)
dict <- read_csv("~/sandboxes/long2wide-dict/test.csv")
dict %>%
filter(!is.na(code)) %>%
mutate(lx_start = ifelse(code == "lx", 1:n(), NA) %>% na.locf()) %>%
arrange(lx_start, code) %>%
group_by(lx_start, code) %>%
mutate(code_start = 1:n()) %>%
unite(code, code, code_start, sep = ".") %>%
spread(code, content) %>%
select(lx_start, lx.1, everything())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment