I need to replace blank values in the second row of my data set with text ("dummy" in this example), without overwriting blank values in the remainder of the dataset.
我需要用文本(本例中为“dummy”)替换数据集第二行中的空白值,而不覆盖数据集其余部分的空白值。
For example, if you start with a dataframe
例如,如果您从数据框开始
df <- data.frame(x1=c("X","Y",""),x2=c("Z","",""),x3=c("A","B","C"),x4=c("E","",""))
I would like a line of code to convert it to:
我想要一行代码将其转换为:
df <- data.frame(x1=c("X","Y",""),x2=c("Z","dummy",""),x3=c("A","B","C"),x4=c("E","dummy",""))
I have tried:
我努力了:
df[df == ""] <- "dummy"
however this replaces all blank values, instead of only those in the second row. I have also tried combining this with df[2,] in various ways with no success.
但是这会替换所有空白值,而不是仅替换第二行中的空白值。我也试过以各种方式将它与df [2,]结合起来但没有成功。
Any suggestions greatly appreciated.
任何建议都非常感谢。
1 个解决方案
#1
1
Perhaps its easier to make things not factors in the first place:
也许最容易让事情不是因素:
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
If you want everything to be a factor once your done you could try:
如果你希望一切都成为一个因素,你可以尝试:
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
EDIT as per your comment:
根据您的评论编辑:
# 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
#1
1
Perhaps its easier to make things not factors in the first place:
也许最容易让事情不是因素:
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
If you want everything to be a factor once your done you could try:
如果你希望一切都成为一个因素,你可以尝试:
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
EDIT as per your comment:
根据您的评论编辑:
# 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