Reputation: 4408
I have a dataset, df that I reading in from Excel to R. For some reason, when reading in the file, R sets all the empty fields to NA. How do I reverse this? I want the NA values in the column to be converted back to empty cells.
Subject Value
hello NA
hello NA
hello NA
I would like:
Subject Value
hello
hello
hello
Here is the dput:
structure(list(Subject = structure(c(1L, 1L, 1L), .Label = "hello", class = "factor"),
Value = c(NA, NA, NA)), class = "data.frame", row.names = c(NA,
-3L))
This is what I have tried:
df[is.na(df$Value)] <- " "
.
I do not know if this structure is correct Any help is appreciated.
Upvotes: 1
Views: 40
Reputation: 887951
We need to assign the same column name
df$Value[is.na(df$Value)] <- ""
Instead, if we do the subset on the whole dataset, it would result in error
df1[is.na(df1$Value)]
Error in
[.data.frame
(df1, is.na(df1$Value)) : undefined columns selected
With tidyverse
, we can also use replace_na
library(dplyr)
library(tidyr)
df1 <- df1 %>%
mutate(Value = replace_na(Value, ""))
df1
# Subject Value
#1 hello
#2 hello
#3 hello
Upvotes: 1