在数据分析中,填充空白并非易事,尤其是当向量包含数字或整数值时。假设我们有一个向量x,其中包含1,,2、3、4、5,并且我们要在第一个值之后将1代替空格,然后cummax函数和seq_along函数可以用作x [cummax(seq_along(x) *(x!=“”))]。
x1<−rep(c(1,"",2,3),times=5) x1
输出结果
[1] "1" "" "2" "3" "1" "" "2" "3" "1" "" "2" "3" "1" "" "2" "3" "1" "" "2" [20] "3"
x1<−x1[cummax(seq_along(x1)*(x1!=""))] x1
输出结果
[1] "1" "1" "2" "3" "1" "1" "2" "3" "1" "1" "2" "3" "1" "1" "2" "3" "1" "1" "2" [20] "3"
x2<−rep(c(5," ",15,20),times=20) x2
输出结果
[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"
x2<−x2[cummax(seq_along(x2)*(x2!=" "))] x2
输出结果
[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"
x3<−rep(c(5,7,15,14,20,2,3,8,9," ",41,35,25),times=10) x3
输出结果
[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"
x3<−x3[cummax(seq_along(x3)*(x3!=" "))] x3
输出结果
[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"
x4<−rep(c(LETTERS[1:4]," ",LETTERS[5:9]),times=12) x4
输出结果
[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"
x4<−x4[cummax(seq_along(x4)*(x4!=" "))] x4
输出结果
[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"
x5<−rep(c("USA","Canada","China"," ","Russia","Belarus"," ","Sudan","Nepal"," ","Turkey"),times=12) x5
输出结果
[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"
x5<−x5[cummax(seq_along(x5)*(x5!=" "))] x5
输出结果
[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"