My first approach was to use na.strings=""
when I read the data in from a csv. This doesn't work for some reason. I also tried:
df[df==''] <- NA
Which gave me an error: Can't use matrix or array for column indexing.
I tried just the column:
df$col[df$col==''] <- NA
This converts every value in the entire dataframe to NA, even though there are values besides empty strings.
Then I tried to use mutate_all
:
replace.empty <- function(a) {
a[a==""] <- NA
}
#dplyr pipe
df %>% mutate_all(funs(replace.empty))
This also converts every value in the entire dataframe to NA.
I suspect something is weird about my "empty" strings since the first method had no effect but I can't figure out what.
EDIT (at request of MKR)
Output of dput(head(df))
:
structure(c("function (x, df1, df2, ncp, log = FALSE) ", "{",
" if (missing(ncp)) ", " .Call(C_df, x, df1, df2, log)",
" else .Call(C_dnf, x, df1, df2, ncp, log)", "}"), .Dim = c(6L,
1L), .Dimnames = list(c("1", "2", "3", "4", "5", "6"), ""), class =
"noquote")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…