将所有0值替换为NA [英] Replace all 0 values to NA

查看:528
本文介绍了将所有0值替换为NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些数字列的数据框.某行的值为0,在统计分析中应将其视为null.在R中将所有0值替换为NULL的最快方法是什么?

I have a dataframe with some numeric columns. Some row has a 0 value which should be considered as null in statistical analysis. What is the fastest way to replace all the 0 value to NULL in R?

推荐答案

将所有零替换为NA:

df[df == 0] <- NA





说明

1..这不是NULL您不应该用零代替的内容.正如?'NULL'中所述,

1. It is not NULL what you should want to replace zeroes with. As it says in ?'NULL',

NULL表示R中的空对象

NULL represents the null object in R

这是唯一的,我想它可以看作是最没有信息和最空的对象. 1 然后,变得不那么惊讶

which is unique and, I guess, can be seen as the most uninformative and empty object.1 Then it becomes not so surprising that

data.frame(x = c(1, NULL, 2))
#   x
# 1 1
# 2 2

也就是说,R不会为此空对象保留任何空间. 2 同时,查看?'NA',我们看到

That is, R does not reserve any space for this null object.2 Meanwhile, looking at ?'NA' we see that

NA是长度为1的逻辑常数,其中包含缺失值 指标.可以将NA强制转换为除raw以外的任何其他向量类型.

NA is a logical constant of length 1 which contains a missing value indicator. NA can be coerced to any other vector type except raw.

重要的是,NA的长度为1,因此R为其保留了一些空间.例如,

Importantly, NA is of length 1 so that R reserves some space for it. E.g.,

data.frame(x = c(1, NA, 2))
#    x
# 1  1
# 2 NA
# 3  2

此外,数据帧结构要求所有列均具有相同数量的元素,以便没有空洞"(即NULL值).

Also, the data frame structure requires all the columns to have the same number of elements so that there can be no "holes" (i.e., NULL values).

现在,从完全删除包含至少一个零的所有行的意义上讲,您可以在数据帧中用NULL替换零.当使用例如varcovcor时,实际上等效于首先用NA替换零并将use的值设置为"complete.obs".但是,通常这不能令人满意,因为它会导致额外的信息丢失.

Now you could replace zeroes by NULL in a data frame in the sense of completely removing all the rows containing at least one zero. When using, e.g., var, cov, or cor, that is actually equivalent to first replacing zeroes with NA and setting the value of use as "complete.obs". Typically, however, this is unsatisfactory as it leads to extra information loss.

2..在解决方案中,我没有使用某种循环,而是使用了df == 0矢量化. df == 0返回(尝试)与df大小相同的矩阵,并带有条目TRUEFALSE.此外,我们还可以将此矩阵传递给子集[...](请参见?'[').最后,尽管df[df == 0]的结果非常直观,但df[df == 0] <- NA给出了预期的效果似乎很奇怪.赋值运算符<-确实并不总是那么聪明,并且不能以这种方式与某些其他对象一起使用,但是对于数据帧却是如此.参见?'<-'.

2. Instead of running some sort of loop, in the solution I use df == 0 vectorization. df == 0 returns (try it) a matrix of the same size as df, with the entries TRUE and FALSE. Further, we are also allowed to pass this matrix to the subsetting [...] (see ?'['). Lastly, while the result of df[df == 0] is perfectly intuitive, it may seem strange that df[df == 0] <- NA gives the desired effect. The assignment operator <- is indeed not always so smart and does not work in this way with some other objects, but it does so with data frames; see ?'<-'.


1 集合论中的空集合有某种联系.
2 与集合理论的另一个相似之处:空集合是每个集合的子集,但我们不为其保留任何空间.

1 The empty set in the set theory feels somehow related.
2 Another similarity with the set theory: the empty set is a subset of every set, but we do not reserve any space for it.

这篇关于将所有0值替换为NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆