Chennai Cheetah
Chennai Cheetah

Reputation: 363

Bind existing data frame with variable logic in R

I have a below Input data frame df and a variable n

  V1 V2 V3
1  1  2 30
2  1  3 10
3  2  5 40
4  3  4 20
5  4  5 30

Based on my n value. I need to append that many rows with the existing data frame. Eg. If n = 5 . I want to append the below values in that manner to my existing data frame

1 1 0
2 2 0
3 3 0
4 4 0
5 5 0

So My Final Expected output will be

  V1 V2 V3
1  1  2 30
2  1  3 10
3  2  5 40
4  3  4 20
5  4  5 30
6  1  1  0
7  2  2  0
8  3  3  0
9  4  4  0
10 5  5  0

Upvotes: 1

Views: 17

Answers (1)

Rory S
Rory S

Reputation: 1298

This should do it:

n <- 5
ndata <- data.frame(
    V1 = 1:n,
    V2 = 1:n,
    V3 = rep(0, n)
)

rbind(df, ndata)

Upvotes: 1

Related Questions