在列中的空字符串中输入值

时间:2016-06-19 18:50:02

标签: r

我需要用字母(“S”)替换列的空白部分。

titanic3$embarked  <- as.character(titanic3$embarked)
> str(titanic3)
titanic3$embarked[is.na(titanic3$embarked)]  <- "S"
> t1  <- select(titanic3, embarked)
> View(t1)

1 个答案:

答案 0 :(得分:0)

您可以使用which功能。我的例子:

# Create data frame with two columns: value and value2. The last has 2 NA values
df <- data.frame("value"= c(1, 3, 5, 7), "value2" = c("A", "B", NA, NA)) 
# Convert column to character
df$value2 <- as.character(df$value2)
# Find which value in column is NA and replace it with "S"  
df$value2[which(is.na(df$value2))] <- "S"

和otuput

> df
  value value2
1     1      A
2     3      B
3     5      S
4     7      S