homeNB outTripNB
1 ANDERLECHT CENTRUM - WAYEZ 2
2 GROTE MARKT 3
Ideally, I would like to have something like this:
homeNB outTripNB toBeAdded
1 ANDERLECHT CENTRUM - WAYEZ 2 NA
2 ANDERLECHT CENTRUM - WAYEZ 2 NA
3 GROTE MARKT 3 NA
4 GROTE MARKT 3 NA
5 GROTE MARKT 3 NA
So as you can see, the number of newly added lines is equal to the number in the column outTripNB. As I have lots of NB in my data so it will be possible for me to add this manually. I am wondering if you can help me with this? Thanks very much for your help
The uncount function in the tidyr package should cover your use case. I added the toBeAdded column filled with NA to match your desired output.
library("tidyr")
library("dplyr")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
exampleData <- data.frame(homeNB = c("ANDERLECHT CENTRUM - WAYEZ", "GROTE MARKT"), outTripNB = c(2, 3))
exampleData <- uncount(exampleData, weights = outTripNB, .remove = FALSE) %>%
mutate(toBeAdded = NA)
exampleData
#> homeNB outTripNB toBeAdded
#> 1 ANDERLECHT CENTRUM - WAYEZ 2 NA
#> 2 ANDERLECHT CENTRUM - WAYEZ 2 NA
#> 3 GROTE MARKT 3 NA
#> 4 GROTE MARKT 3 NA
#> 5 GROTE MARKT 3 NA