# How to replace blanks in a vector with the previous element in R?

Filling of blanks is not an easy task in data analysis, especially if the vector contains numerical or integer values. Suppose we have a vector x that contains 1, , 2, 3, 4, 5 and we want to put 1 in place of blank after first value then cummax function along with seq_along function can be used as x[cummax(seq_along(x)*(x!=""))].

## Example1

Live Demo

x1<−rep(c(1,"",2,3),times=5)
x1

## Output

[1]  "1" "" "2" "3" "1" "" "2" "3" "1" "" "2" "3" "1" "" "2" "3" "1" "" "2"
[20] "3"

## Example

Live Demo

x1<−x1[cummax(seq_along(x1)*(x1!=""))]
x1

## Output

[1]  "1" "1" "2" "3" "1" "1" "2" "3" "1" "1" "2" "3" "1" "1" "2" "3" "1" "1" "2"
[20] "3"

## Example2

x2<−rep(c(5," ",15,20),times=20)
x2

## Output

[1]  "5" " " "15" "20" "5" " " "15" "20" "5" " " "15" "20" "5" " " "15"
[16] "20" "5" " " "15" "20" "5" " " "15" "20" "5" " " "15" "20" "5" " "
[31] "15" "20" "5" " " "15" "20" "5" " " "15" "20" "5" " " "15" "20" "5"
[46] " " "15" "20" "5" " " "15" "20" "5" " " "15" "20" "5" " " "15" "20"
[61] "5" " " "15" "20" "5" " " "15" "20" "5" " " "15" "20" "5" " " "15"
[76] "20" "5" " " "15" "20"

## Example

x2<−x2[cummax(seq_along(x2)*(x2!=" "))]
x2

## Output

[1] "5" "5" "15" "20" "5" "5" "15" "20" "5" "5" "15" "20" "5" "5" "15"
[16] "20" "5" "5" "15" "20" "5" "5" "15" "20" "5" "5" "15" "20" "5" "5"
[31] "15" "20" "5" "5" "15" "20" "5" "5" "15" "20" "5" "5" "15" "20" "5"
[46] "5" "15" "20" "5" "5" "15" "20" "5" "5" "15" "20" "5" "5" "15" "20"
[61] "5" "5" "15" "20" "5" "5" "15" "20" "5" "5" "15" "20" "5" "5" "15"
[76] "20" "5" "5" "15" "20"

## Example3

x3<−rep(c(5,7,15,14,20,2,3,8,9," ",41,35,25),times=10)
x3

## Output

[1] "5" "7" "15" "14" "20" "2" "3" "8" "9" " " "41" "35" "25" "5" "7"
[16] "15" "14" "20" "2" "3" "8" "9" " " "41" "35" "25" "5" "7" "15" "14"
[31] "20" "2" "3" "8" "9" " " "41" "35" "25" "5" "7" "15" "14" "20" "2"
[46] "3" "8" "9" " " "41" "35" "25" "5" "7" "15" "14" "20" "2" "3" "8"
[61] "9" " " "41" "35" "25" "5" "7" "15" "14" "20" "2" "3" "8" "9" " "
[76] "41" "35" "25" "5" "7" "15" "14" "20" "2" "3" "8" "9" " " "41" "35"
[91] "25" "5" "7" "15" "14" "20" "2" "3" "8" "9" " " "41" "35" "25" "5"
[106] "7" "15" "14" "20" "2" "3" "8" "9" " " "41" "35" "25" "5" "7" "15"
[121] "14" "20" "2" "3" "8" "9" " " "41" "35" "25"

## Example

x3<−x3[cummax(seq_along(x3)*(x3!=" "))]
x3

## Output

[1] "5" "7" "15" "14" "20" "2" "3" "8" "9" "9" "41" "35" "25" "5" "7"
[16] "15" "14" "20" "2" "3" "8" "9" "9" "41" "35" "25" "5" "7" "15" "14"
[31] "20" "2" "3" "8" "9" "9" "41" "35" "25" "5" "7" "15" "14" "20" "2"
[46] "3" "8" "9" "9" "41" "35" "25" "5" "7" "15" "14" "20" "2" "3" "8"
[61] "9" "9" "41" "35" "25" "5" "7" "15" "14" "20" "2" "3" "8" "9" "9"
[76] "41" "35" "25" "5" "7" "15" "14" "20" "2" "3" "8" "9" "9" "41" "35"
[91] "25" "5" "7" "15" "14" "20" "2" "3" "8" "9" "9" "41" "35" "25" "5"
[106] "7" "15" "14" "20" "2" "3" "8" "9" "9" "41" "35" "25" "5" "7" "15"
[121] "14" "20" "2" "3" "8" "9" "9" "41" "35" "25"

## Example4

x4<−rep(c(LETTERS[1:4]," ",LETTERS[5:9]),times=12)
x4

## Output

[1] "A" "B" "C" "D" " " "E" "F" "G" "H" "I" "A" "B" "C" "D" " " "E" "F" "G"
[19] "H" "I" "A" "B" "C" "D" " " "E" "F" "G" "H" "I" "A" "B" "C" "D" " " "E"
[37] "F" "G" "H" "I" "A" "B" "C" "D" " " "E" "F" "G" "H" "I" "A" "B" "C" "D"
[55] " " "E" "F" "G" "H" "I" "A" "B" "C" "D" " " "E" "F" "G" "H" "I" "A" "B"
[73] "C" "D" " " "E" "F" "G" "H" "I" "A" "B" "C" "D" " " "E" "F" "G" "H" "I"
[91] "A" "B" "C" "D" " " "E" "F" "G" "H" "I" "A" "B" "C" "D" " " "E" "F" "G"
[109] "H" "I" "A" "B" "C" "D" " " "E" "F" "G" "H" "I"

## Example

x4<−x4[cummax(seq_along(x4)*(x4!=" "))]
x4

## Output

[1] "A" "B" "C" "D" "D" "E" "F" "G" "H" "I" "A" "B" "C" "D" "D" "E" "F" "G"
[19] "H" "I" "A" "B" "C" "D" "D" "E" "F" "G" "H" "I" "A" "B" "C" "D" "D" "E"
[37] "F" "G" "H" "I" "A" "B" "C" "D" "D" "E" "F" "G" "H" "I" "A" "B" "C" "D"
[55] "D" "E" "F" "G" "H" "I" "A" "B" "C" "D" "D" "E" "F" "G" "H" "I" "A" "B"
[73] "C" "D" "D" "E" "F" "G" "H" "I" "A" "B" "C" "D" "D" "E" "F" "G" "H" "I"
[91] "A" "B" "C" "D" "D" "E" "F" "G" "H" "I" "A" "B" "C" "D" "D" "E" "F" "G"
[109] "H" "I" "A" "B" "C" "D" "D" "E" "F" "G" "H" "I"

## Example5

x5<−rep(c("USA","Canada","China"," ","Russia","Belarus"," ","Sudan","Nepal"," ","Turkey"),times=12)
x5

## Output

[1] "USA" "Canada" "China" " " "Russia" "Belarus" " "
[8] "Sudan" "Nepal" " " "Turkey" "USA" "Canada" "China"
[15] " " "Russia" "Belarus" " " "Sudan" "Nepal" " "
[22] "Turkey" "USA" "Canada" "China" " " "Russia" "Belarus"
[29] " " "Sudan" "Nepal" " " "Turkey" "USA" "Canada"
[36] "China" " " "Russia" "Belarus" " " "Sudan" "Nepal"
[43] " " "Turkey" "USA" "Canada" "China" " " "Russia"
[50] "Belarus" " " "Sudan" "Nepal" " " "Turkey" "USA"
[57] "Canada" "China" " " "Russia" "Belarus" " " "Sudan"
[64] "Nepal" " " "Turkey" "USA" "Canada" "China" " "
[71] "Russia" "Belarus" " " "Sudan" "Nepal" " " "Turkey"
[78] "USA" "Canada" "China" " " "Russia" "Belarus" " "
[85] "Sudan" "Nepal" " " "Turkey" "USA" "Canada" "China"
[92] " " "Russia" "Belarus" " " "Sudan" "Nepal" " "
[99] "Turkey" "USA" "Canada" "China" " " "Russia" "Belarus"
[106] " " "Sudan" "Nepal" " " "Turkey" "USA" "Canada"
[113] "China" " " "Russia" "Belarus" " " "Sudan" "Nepal"
[120] " " "Turkey" "USA" "Canada" "China" " " "Russia"
[127] "Belarus" " " "Sudan" "Nepal" " " "Turkey"

## Example

x5<−x5[cummax(seq_along(x5)*(x5!=" "))]
x5

## Output

[1] "USA" "Canada" "China" "China" "Russia" "Belarus" "Belarus"
[8] "Sudan" "Nepal" "Nepal" "Turkey" "USA" "Canada" "China"
[15] "China" "Russia" "Belarus" "Belarus" "Sudan" "Nepal" "Nepal"
[22] "Turkey" "USA" "Canada" "China" "China" "Russia" "Belarus"
[29] "Belarus" "Sudan" "Nepal" "Nepal" "Turkey" "USA" "Canada"
[36] "China" "China" "Russia" "Belarus" "Belarus" "Sudan" "Nepal"
[43] "Nepal" "Turkey" "USA" "Canada" "China" "China" "Russia"
[50] "Belarus" "Belarus" "Sudan" "Nepal" "Nepal" "Turkey" "USA"
[57] "Canada" "China" "China" "Russia" "Belarus" "Belarus" "Sudan"
[64] "Nepal" "Nepal" "Turkey" "USA" "Canada" "China" "China"
[71] "Russia" "Belarus" "Belarus" "Sudan" "Nepal" "Nepal" "Turkey"
[78] "USA" "Canada" "China" "China" "Russia" "Belarus" "Belarus"
[85] "Sudan" "Nepal" "Nepal" "Turkey" "USA" "Canada" "China"
[92] "China" "Russia" "Belarus" "Belarus" "Sudan" "Nepal" "Nepal"
[99] "Turkey" "USA" "Canada" "China" "China" "Russia" "Belarus"
[106] "Belarus" "Sudan" "Nepal" "Nepal" "Turkey" "USA" "Canada"
[113] "China" "China" "Russia" "Belarus" "Belarus" "Sudan" "Nepal"
[120] "Nepal" "Turkey" "USA" "Canada" "China" "China" "Russia"
[127] "Belarus" "Belarus" "Sudan" "Nepal" "Nepal" "Turkey"

Updated on: 17-Oct-2020

95 Views