Skip to content

Instantly share code, notes, and snippets.

@brunaw
Created April 13, 2021 13:16
Show Gist options
  • Save brunaw/8b3b3530980ea7fb7876658bef79fbd9 to your computer and use it in GitHub Desktop.
Save brunaw/8b3b3530980ea7fb7876658bef79fbd9 to your computer and use it in GitHub Desktop.
library(tidyverse)
data <- data.frame(
cidade =
c("cidade_1", "urbana", "rural",
"cidade_2", "urbana", "rural",
"cidade_3", "urbana", "rural"),
valor_1 = rpois(9, 100),
valor_2 = rpois(9, 1000))
data <- data %>%
mutate(indice = rep(1:(nrow(data)/3), each = 3))
urb_rural <- data %>%
filter(cidade %in% c("urbana", "rural")) %>%
arrange(indice) %>%
pivot_wider(names_from = cidade, values_from = c(valor_1, valor_2)) %>%
select(-indice)
final_data <- data %>%
filter(!(cidade %in% c("urbana", "rural"))) %>%
bind_cols(urb_rural) %>%
select(1, 2, 5, 6, 3, -4, 7, 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment