检测向量中的空字符串,并用其前一个元素替换每个空字符串

时间:2018-05-22 17:40:57

标签: r

假设我有这个载体:

vector <- c("hello", "", "sometimes!","", "goodbye", "okay")

我想用前一个项替换每个空字符串。

起初,我考虑过使用

Library(stringi) #package
stri_isempty(vector)
[1] FALSE  TRUE FALSE  TRUE FALSE FALSE

知道哪个元素是空的,但是现在,我被卡住了。

我想得到这个结果:

[1] "hello"      "hello"      "sometimes!" "sometimes"  "goodbye"    "okay" 

接下来我该怎么做?

谢谢

编辑:另外,如果我想添加

,我应该在建议的答案中添加什么
"final"

在每个空字符串前面?

编辑2:我能够使用Erik Kornet的代码在前一项的前面添加另一个字符串:

for (v in 1:length(vector)){
    if (vector[v] == ""){
      vector[v] = paste("final", vector[v-1])
    }
}

4 个答案:

答案 0 :(得分:3)

当您想要使用移位值替换时,一个好习惯是创建一个移位向量以避免使用循环并直接使用布尔选择:

vector <- c("hello", "", "sometimes!","", "goodbye", "okay")
vector_shift <- c(NA,vector[1:length(vector)]) # create a vactor of same length but shifted
vector[vector == ""] <- vector_shift[vector == ""]

[1] "hello"      "hello"      "sometimes!" "sometimes!" "goodbye"    "okay"    

答案 1 :(得分:3)

我们可以将""替换为NA,然后使用na.locf包中的zoo

vec <- c("hello", "", "sometimes!","", "goodbye", "okay")

library(zoo)
vec[vec %in% ""] <- NA
vec2 <- na.locf(vec)
vec2
# [1] "hello"      "hello"      "sometimes!" "sometimes!" "goodbye"    "okay"

答案 2 :(得分:2)

您可以使用简单的for循环执行此操作,将vector[x]替换为vector[x-1],其中vector[x] == ""

vector <- c("hello", "", "sometimes!","", "goodbye", "okay")

for (v in 1:length(vector)){
    if (vector[v] == ""){
      vector[v] = vector[v-1]
    }
}

> vector
[1] "hello"      "hello"      "sometimes!" "sometimes!" "goodbye"
[6] "okay"

答案 3 :(得分:1)

如果您一次最多只丢失1个,则可以尝试:

library(stringi)
library(data.table) # alternatively you can use library(DataCombine) with shift(vector, -1) below
vector<- ifelse(stri_isempty(vector), shift(vector, 1), vector)

现在,如果你有更多,例如:

vector <- c("hello", "", "", "", "sometimes!","", "goodbye", "okay")
vector
#[1] "hello"      ""           ""           ""           "sometimes!" ""           "goodbye"   
#[8] "okay"

while循环将执行此操作:

while(any(stri_isempty(vector))){
   vector<- ifelse(stri_isempty(vector), shift(vector, 1), vector)
}
vector
#[1] "hello"      "hello"      "hello"      "hello"      "sometimes!" "sometimes!" "goodbye"   
#[8] "okay" 

最后,如果它位于数据框中,您可以使用FillDown中的DataCombine函数(但它有点时髦,请注意)

library(DataCombine)
df<- data.frame(id = 1:length(vector), vector=as.character(vector))
df$vector<- ifelse(stri_isempty(df$vector), NA, as.character(df$vector))
FillDown(df, "vector")
# id     vector
#1  1      hello
#2  2      hello
#3  3      hello
#4  4      hello
#5  5 sometimes!
#6  6 sometimes!
#7  7    goodbye
#8  8       okay