How to add zero before a vector in R?

R ProgrammingServer Side ProgrammingProgramming

To add zero before a vector in R, we can simply use rep function and provide the number times we want to have zero in the vector.

For Example, if we have a vector called X that contains three values say 1, 2, 3 and we want to add three zeros before this vector then we can use the command as given below −

X<-c(rep(0,3),X)

The resulting vector will be 0, 0, 0, 1, 2, 3.

Example 1

Following snippet creates a sample data frame −

x1<-round(rnorm(200),0)
x1

The following dataframe is created

[1] 0 0 0 2 0 1 0 1 -1 0 -1 1 -1 -2 -1 0 0 -1 1 1 0 -1 1 0 0
[26] 1 0 0 0 1 1 0 -1 -2 2 0 1 1 -1 0 -1 0 0 0 0 0 -1 0 0 -
1
[51] 0 1 0 0 0 -2 1 0 -2 -1 -1 -1 0 0 1 0 0 1 1 2 0 2 1 1
1
[76] 0 3 -2 -1 0 -1 1 1 0 0 -1 0 0 1 0 -1 3 1 -1 0 1 0 1 -1
0
[101] 0 -1 0 -1 0 1 1 0 -2 1 1 -1 1 0 1 -1 0 1 0 1 0 0 1 -2 -
1
[126] 0 0 0 -1 1 0 -1 -2 1 -1 1 1 -1 0 2 1 0 1 -1 0 0 -1 3 0
2
[151] -1 0 0 1 1 0 0 0 0 0 0 1 -2 0 1 -2 1 -2 -1 0 1 2 0 -1
0
[176] 1 0 0 1 2 -2 2 1 3 0 -1 -1 -3 1 0 -1 0 0 1 1 0 0 0 2
0

To add zero before a vector on the above created data frame, add the following code to the above snippet −

x1<-round(rnorm(200),0)
x1<-c(rep(0,10),x1)
x1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 1 -1 0 -1 1 -1 -2 -1
[26] 0 0 -1 1 1 0 -1 1 0 0 1 0 0 0 1 1 0 -1 -2 2 0 1 1 -1
0
[51] -1 0 0 0 0 0 -1 0 0 -1 0 1 0 0 0 -2 1 0 -2 -1 -1 -1 0 0
1
[76] 0 0 1 1 2 0 2 1 1 1 0 3 -2 -1 0 -1 1 1 0 0 -1 0 0 1
0
[101] -1 3 1 -1 0 1 0 1 -1 0 0 -1 0 -1 0 1 1 0 -2 1 1 -1 1 0
1
[126] -1 0 1 0 1 0 0 1 -2 -1 0 0 0 -1 1 0 -1 -2 1 -1 1 1 -1 0
2
[151] 1 0 1 -1 0 0 -1 3 0 2 -1 0 0 1 1 0 0 0 0 0 0 1 -2 0
1
[176] -2 1 -2 -1 0 1 2 0 -1 0 1 0 0 1 2 -2 2 1 3 0 -1 -1 -3 1
0
[201] -1 0 0 1 1 0 0 0 2 0

Example 2

Following snippet creates a sample data frame −

x2<-rpois(200,1)
x2

The following dataframe is created

[1] 0 0 2 2 4 2 1 0 0 3 1 1 2 1 2 1 1 0 0 3 0 1 1 1 2 0 2 0 1 2 0 0 0 4 1 1 1
[38] 1 1 0 3 1 0 1 1 1 0 0 1 0 1 0 2 0 2 1 1 0 1 1 0 2 0 2 0 0 1 0 2 1 2 1 2 1
[75] 0 1 1 2 3 1 3 1 2 1 1 0 1 0 0 0 5 1 0 2 3 0 2 1 0 0 0 1 0 1 0 0 0 0 0 1 1
[112] 0 0 0 0 1 0 2 4 0 2 0 2 0 0 2 2 2 0 0 0 0 0 0 1 0 1 1 2 3 2 0 0 1 0 0 1 2
[149] 3 1 1 0 0 3 1 1 1 2 1 1 0 1 1 1 0 1 0 0 0 0 1 1 0 2 1 0 2 1 0 1 0 0 0 0 1
[186] 0 0 0 1 0 0 0 1 0 3 3 2 2 1 2

To add zero before a vector on the above created data frame, add the following code to the above snippet −

x2<-rpois(200,1)
x2<-c(rep(0,10),x2)
x2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0 0 0 0 0 0 0 0 0 0 0 0 2 2 4 2 1 0 0 3 1 1 2 1 2 1 1 0 0 3 0 1 1 1 2 0 2
[38] 0 1 2 0 0 0 4 1 1 1 1 1 0 3 1 0 1 1 1 0 0 1 0 1 0 2 0 2 1 1 0 1 1 0 2 0 2
[75] 0 0 1 0 2 1 2 1 2 1 0 1 1 2 3 1 3 1 2 1 1 0 1 0 0 0 5 1 0 2 3 0 2 1 0 0 0
[112] 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 2 4 0 2 0 2 0 0 2 2 2 0 0 0 0 0 0 1 0 1 1
[149] 2 3 2 0 0 1 0 0 1 2 3 1 1 0 0 3 1 1 1 2 1 1 0 1 1 1 0 1 0 0 0 0 1 1 0 2 1
[186] 0 2 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 3 3 2 2 1 2

Example 3

Following snippet creates a sample data frame −

x3<-rpois(200,10)
x3

The following dataframe is created

[1] 13 14 8 7 14 12 14 15 10 14 8 4 16 11 5 13 12 11 4 6 5 9 9 11 11
[26] 5 12 8 15 7 6 10 10 7 5 5 8 3 6 9 12 9 13 11 7 6 16 5 6
12
[51] 7 7 5 6 11 7 7 13 5 2 6 9 13 13 6 11 12 17 9 14 10 7 10 11
14
[76] 9 19 4 15 6 7 13 19 9 11 12 8 10 21 15 13 7 8 10 16 14 7 5 8
8
[101] 13 11 14 8 8 7 15 7 8 16 13 7 3 6 11 6 9 11 14 12 7 5 5 10
20
[126] 9 9 10 14 9 6 10 9 11 12 11 14 8 10 15 4 6 10 12 8 10 9 6 5
11
[151] 6 17 9 8 7 9 19 8 9 9 8 5 5 13 8 14 6 10 14 11 9 6 11 11
14
[176] 10 7 7 11 9 11 8 6 6 13 5 14 9 9 16 16 9 6 11 11 8 7 11 9
8

To add zero before a vector on the above created data frame, add the following code to the above snippet −

x3<-rpois(200,10)
x3<-c(rep(0,50),x3)
x3

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
[26] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
[51] 13 14 8 7 14 12 14 15 10 14 8 4 16 11 5 13 12 11 4 6 5 9 9 11
11
[76] 5 12 8 15 7 6 10 10 7 5 5 8 3 6 9 12 9 13 11 7 6 16 5 6
12
[101] 7 7 5 6 11 7 7 13 5 2 6 9 13 13 6 11 12 17 9 14 10 7 10 11
14
[126] 9 19 4 15 6 7 13 19 9 11 12 8 10 21 15 13 7 8 10 16 14 7 5 8
8
[151] 13 11 14 8 8 7 15 7 8 16 13 7 3 6 11 6 9 11 14 12 7 5 5 10
20
[176] 9 9 10 14 9 6 10 9 11 12 11 14 8 10 15 4 6 10 12 8 10 9 6 5
11
[201] 6 17 9 8 7 9 19 8 9 9 8 5 5 13 8 14 6 10 14 11 9 6 11 11
14
[226] 10 7 7 11 9 11 8 6 6 13 5 14 9 9 16 16 9 6 11 11 8 7 11 9
8

Example 4

Following snippet creates a sample data frame −

x4<-rexp(50)
x4

The following dataframe is created

[1] 0.096339494 0.238162347 0.040384842 0.605404445 0.241478754 1.080798495
[7] 0.709655503 1.120359703 0.060613286 1.174646981 0.281041093 0.064124504
[13] 0.029893382 1.999286495 0.973140290 2.683303086 1.558834251 0.702950122
[19] 0.328594336 1.115729940 0.453112570 1.349650233 0.886071674 0.373609728
[25] 1.694564143 2.367704071 0.185704205 0.931796643 0.770835058 1.898470368
[31] 1.425884338 0.005417023 0.068695573 0.012893298 1.527094753 2.653356718
[37] 1.357244943 0.094442877 0.067177774 0.348716944 0.203150074 2.030599111
[43] 2.536209362 0.030516792 2.307241185 3.855584195 3.105160649 0.087840714
[49] 0.381441276 0.341276379

To add zero before a vector on the above created data frame, add the following code to the above snippet −

x4<-rexp(50)
x4<-c(rep(0,25),x4)
x4

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
[7] 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
[13] 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
[19] 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
[25] 0.000000000 0.096339494 0.238162347 0.040384842 0.605404445 0.241478754
[31] 1.080798495 0.709655503 1.120359703 0.060613286 1.174646981 0.281041093
[37] 0.064124504 0.029893382 1.999286495 0.973140290 2.683303086 1.558834251
[43] 0.702950122 0.328594336 1.115729940 0.453112570 1.349650233 0.886071674
[49] 0.373609728 1.694564143 2.367704071 0.185704205 0.931796643 0.770835058
[55] 1.898470368 1.425884338 0.005417023 0.068695573 0.012893298 1.527094753
[61] 2.653356718 1.357244943 0.094442877 0.067177774 0.348716944 0.203150074
[67] 2.030599111 2.536209362 0.030516792 2.307241185 3.855584195 3.105160649
[73] 0.087840714 0.381441276 0.341276379

Example 5

Following snippet creates a sample data frame −

x5<-round(rnorm(100),2)
x5

The following dataframe is created

[1] 1.37 0.91 -0.12 0.00 -0.10 1.65 1.62 0.39 0.56 0.37 0.18 0.31
[13] 0.12 -0.33 0.10 1.05 0.09 -0.16 0.40 1.31 1.84 1.23 0.90 2.56
[25] -1.12 -0.04 1.48 0.14 -0.23 0.89 -0.25 0.30 0.40 -0.48 -0.42 0.03
[37] 1.51 0.93 -0.02 -0.71 -0.33 1.49 1.08 1.88 0.57 -0.12 -0.85 1.61
[49] 0.45 -0.75 1.05 -0.36 0.01 0.72 2.00 -0.64 -1.68 -1.90 -0.18 -0.79
[61] 1.22 -2.17 -1.86 0.04 0.22 1.18 0.14 1.33 0.97 1.31 -1.11 1.22
[73] -0.08 0.46 -0.50 -0.16 -1.17 -0.42 0.24 -0.63 0.09 -0.41 -0.63 0.12
[85] -1.19 -1.31 2.29 1.84 0.88 1.61 -0.45 0.28 0.42 1.40 0.52 -0.73
[97] 0.06 1.02 -0.91 -2.40

To add zero before a vector on the above created data frame, add the following code to the above snippet −

x5<-round(rnorm(100),2)
x5<-c(rep(0,25),x5)
x5

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[13] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[25] 0.00 1.37 0.91 -0.12 0.00 -0.10 1.65 1.62 0.39 0.56 0.37 0.18
[37] 0.31 0.12 -0.33 0.10 1.05 0.09 -0.16 0.40 1.31 1.84 1.23 0.90
[49] 2.56 -1.12 -0.04 1.48 0.14 -0.23 0.89 -0.25 0.30 0.40 -0.48 -0.42
[61] 0.03 1.51 0.93 -0.02 -0.71 -0.33 1.49 1.08 1.88 0.57 -0.12 -0.85
[73] 1.61 0.45 -0.75 1.05 -0.36 0.01 0.72 2.00 -0.64 -1.68 -1.90 -0.18
[85] -0.79 1.22 -2.17 -1.86 0.04 0.22 1.18 0.14 1.33 0.97 1.31 -1.11
[97] 1.22 -0.08 0.46 -0.50 -0.16 -1.17 -0.42 0.24 -0.63 0.09 -0.41 -0.63
[109] 0.12 -1.19 -1.31 2.29 1.84 0.88 1.61 -0.45 0.28 0.42 1.40 0.52
[121] -0.73 0.06 1.02 -0.91 -2.40
raja
Updated on 03-Nov-2021 05:04:49

Advertisements