用文本替换一行中的空白值

时间:2013-12-05 14:03:36

标签: r

我需要用文本替换数据集第二行中的空白值(本例中为“dummy”),而不覆盖数据集其余部分的空白值。

例如,如果您从数据框开始

df <- data.frame(x1=c("X","Y",""),x2=c("Z","",""),x3=c("A","B","C"),x4=c("E","",""))

我想要一行代码将其转换为:

df <- data.frame(x1=c("X","Y",""),x2=c("Z","dummy",""),x3=c("A","B","C"),x4=c("E","dummy",""))

我试过了:

df[df == ""] <- "dummy"

然而,这会替换所有空白值,而不是仅替换第二行中的空白值。 我也试过以各种方式将它与df [2,]结合起来但没有成功。

任何建议都非常感谢。

1 个答案:

答案 0 :(得分:1)

也许更容易让事情变得不是因素:

df <- data.frame(x1=c("X","Y",""),x2=c("Z","",""), 
                 x3=c("A","B","C"),x4=c("E","",""), 
                 stringsAsFactors = FALSE)

df[df == ""] <- "YO"
#df
#  x1 x2 x3 x4
#1  X  Z  A  E
#2  Y YO  B YO
#3 YO YO  C YO

如果你希望一切都成为一个因素,你可以尝试:

df <- data.frame(x1=c("X","Y",""), x2=c("Z","",""), 
                 x3=c("A","B","C"), x4=c("E","",""))

new <- as.data.frame( sapply(df, function(x){ w <- as.character(x)
                                              w[w == ""] <- "DUMMY"
                                              return(w) } ))

str(new)
#'data.frame':   3 obs. of  4 variables:
# $ x1: Factor w/ 3 levels "DUMMY","X","Y": 2 3 1
# $ x2: Factor w/ 2 levels "DUMMY","Z": 2 1 1
# $ x3: Factor w/ 3 levels "A","B","C": 1 2 3
# $ x4: Factor w/ 2 levels "DUMMY","E": 2 1 1

根据您的评论编辑:

# you still need to set things to characters first
df <- data.frame(x1=c("X","Y",""),x2=c("Z","",""), 
                 x3=c("A","B","C"),x4=c("E","",""), 
                 stringsAsFactors = FALSE)

# then
df[2,][df[2,] == ""] <- "DUMMY"

df
#  x1    x2 x3    x4
#1  X     Z  A     E
#2  Y DUMMY  B DUMMY
#3           C