2

I have a data table that looks like this:

cep3 <- structure(list(lat = c(-23.39429, -23.39988, -23.38233, -23.39009, -23.40135, -23.4019), lon = c(-46.3277, -46.30741, -46.30328, -46.31071, -46.32225, -46.32242), jobs = c(3, 1, 127, 2, 11, 2)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
head(cep3)

I want to get n copies of each line (n = job), in a way that each line represents one job, but maintaining the lat/lon, so that when i plot it each line represents one point.

Is there a simple way to do it? I coudn't find any solutions.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Luis F.
  • 45
  • 6

1 Answers1

2

One option is uncount

library(tidyverse)
cep3 %>% 
    uncount(jobs)

If we need to keep the 'jobs' column, use .remove = FALSE (by default it is TRUE)

cep3 %>% 
   uncount(jobs, .remove = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662