如何在 R 中用前一个元素替换向量中的空白?


在数据分析中,填充空白并非易事,尤其是当向量包含数字或整数时。假设我们有一个包含 1、null、2、3、4、5 的向量 x,我们希望在第一个值后将 null 替换为 1,那么可以将 cummax 函数与 seq_along 函数结合使用,即 x[cummax(seq_along(x)*(x!=""))]。

示例 1

 在线演示

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"

示例 2

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"

示例 3

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"

示例 4

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"

示例 5

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"

更新日期:2020 年 10 月 17 日

126 次浏览

开启你的职业生涯

完成课程,获得认证

开始
广告