[英]How to convert special value in R data-frame to NA?
I have a data-frame
containing #
as a missing value in multiple columns. 我有一个
data-frame
其中包含#
作为多列中的缺失值。 How can I convert all such #
s to NA
s? 如何将所有此类
#
转换为NA
?
You can do this a few ways. 您可以通过几种方法来执行此操作。 One is to re-read the file in with the
na.strings
argument set to "#"
一种方法是将
na.strings
参数设置为"#"
以重新读取文件
read.table(file, na.strings = "#")
Another would be to just change the values in the data frame df
with 另一方法是仅使用以下命令更改数据帧
df
的值
df[df == "#"] <- NA
I have written a function makemeNA
that is part of my "SOfun" package . 我已经写了一个函数
makemeNA
,它是我的“ SOfun”包的一部分 。
The function looks like this (in case you don't want to get the package just for this function): 该函数看起来像这样(如果您不想仅为此函数获取软件包):
makemeNA <- function (mydf, NAStrings, fixed = TRUE) {
if (!isTRUE(fixed)) {
mydf[] <- lapply(mydf, function(x) gsub(NAStrings, "", x))
NAStrings <- ""
}
mydf[] <- lapply(mydf, function(x) type.convert(
as.character(x), na.strings = NAStrings))
mydf
}
Usage would be: 用法是:
makemeNA(df, "#")
Get the package with: 通过以下方式获取软件包:
library(devtools)
install_github("mrdwab/SOfun")
is.na(dat) <- dat == "#"
可以解决问题(其中dat
是数据框的名称)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.