将空白更改为NA的功能 [英] Function to change blanks to NA
问题描述
我正在尝试编写一个将空字符串转换为NA的函数.我的专栏之一的摘要如下:
I'm trying to write a function that turns empty strings into NA. A summary of one of my column looks like this:
a b
12 210 468
我想将12个空值更改为NA.我还想将其他空值更改为NA,因此我从这里到那里借了一些东西来解决这个问题:
I'd like to change the 12 empty values to NA. I also have a few other factor columns for which I'd like to change empty values to NA, so I borrowed some stuff from here and there to come up with this:
# change nulls to NAs
nullToNA <- function(df){
# split df into numeric & non-numeric functions
a<-df[,sapply(df, is.numeric), drop = FALSE]
b<-df[,sapply(df, Negate(is.numeric)), drop = FALSE]
# Change empty strings to NA
b<-b[lapply(b,function(x) levels(x) <- c(levels(x), NA) ),] # add NA level
b<-b[lapply(b,function(x) x[x=="",]<- NA),] # change Null to NA
# Put the columns back together
d<-cbind(a,b)
d[, names(df)]
}
但是,我遇到此错误:
> foo<-nullToNA(bar)
Error in x[x == "", ] <- NA : incorrect number of subscripts on matrix
Called from: FUN(X[[i]], ...)
我尝试了以下答案:将所有0值替换为NA ,但它会将我所有的列都更改为数值.
I have tried the answer found here: Replace all 0 values to NA but it changes all my columns to numeric values.
推荐答案
您可以直接索引与逻辑条件匹配的字段.所以你可以这样写:
You can directly index fields that match a logical criterion. So you can just write:
df[is_empty(df)] = NA
is_empty
是您的比较对象,例如df == ""
:
Where is_empty
is your comparison, e.g. df == ""
:
df[df == ""] = NA
但是请注意,is.null(df)
将不起作用,并且反而会很奇怪 1 .我建议不要合并不同类型的列的逻辑!相反,请分别处理它们.
But note that is.null(df)
won’t work, and would be weird anyway1. I would advise against merging the logic for columns of different types, though! Instead, handle them separately.
1 您几乎永远不会在表中遇到NULL
,因为只有在基础向量是list
时,该表才有效.您可以创建具有此约束的矩阵和data.frames,但是is.null(df)
永远不会是TRUE
,因为NULL
值包装在列表中).
1 You’ll almost never encounter NULL
inside a table since that only works if the underlying vector is a list
. You can create matrices and data.frames with this constraint, but then is.null(df)
will never be TRUE
because the NULL
values are wrapped inside the list).
这篇关于将空白更改为NA的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!