Produce Certain Number of Lines based on Known Data in R

Hello everyone,

I am wondering if there is an existing function in R that can produce a certain number of blank lines in R. I will give an example as below。

First, this is an example data frame:

exampleData <- data.frame(homeNB = c("ANDERLECHT CENTRUM - WAYEZ", "GROTE MARKT"), outTripNB = c(2, 3))

The table will look like this:

  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 :slight_smile:

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

Created on 2021-08-06 by the reprex package (v2.0.1)

You can accomplish with a simple call to rep.

exampleData <- data.frame(homeNB = c("ANDERLECHT CENTRUM - WAYEZ", "GROTE MARKT"), 
                          outTripNB = c(2, 3))

newIndex <- rep(1:nrow(exampleData), exampleData$outTripNB)
newIndex
#> [1] 1 1 2 2 2
newData <- exampleData[newIndex, ]
newData
#>                         homeNB outTripNB
#> 1   ANDERLECHT CENTRUM - WAYEZ         2
#> 1.1 ANDERLECHT CENTRUM - WAYEZ         2
#> 2                  GROTE MARKT         3
#> 2.1                GROTE MARKT         3
#> 2.2                GROTE MARKT         3

Created on 2021-08-06 by the reprex package (v1.0.0)

Thanks everyone, both methods have perfectly solved my question! :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.