将(空)数值更改为0

时间:2016-01-21 22:19:47

标签: r

我已经四处寻找我认为简单的解决方案,但迄今为止没有任何证据有用。这是我想做的事情:

1)分别计算特定数据帧列中3个不同字符串的总数,2)将这3个计数放入新的向量中,3)将该向量添加到另一个数据帧。这是我尝试过的:

> x <- c("Type1", "Type1", "Type2", "Type2")
> Column_Counts <- table(x)
> Type_One <- Column_Counts[names(Column_Counts)=="Type1"]
> Type_One <- as.numeric(Type_One)
> Type_Two <- Column_Counts[names(Column_Counts)=="Type2"]
> Type_Two <- as.numeric(Type_Two)
> Type_Three <- Column_Counts[names(PS_Count)=="Type3"]
> Type_Three <- as.numeric(Type_Three)
> New_Vector <- c(Type_One, Type_Two, Type_Three)

以下是问题:有时候“Type3”没有显示在我的报告中,如果是这样,我需要将它计为零,但Type_Three的值为“numeric(empty)”并且不会添加到新的载体。

> New_vector
[1] 2 2
#I need New_vector to be [1] 2, 2, 0

我需要Type_Three = 0,而不是“numeric(empty)”。这是我试过的:

> if (Type_Three == "(empty)")
{Type_Three <- 0}
#Error in if (CR_Des == "(empty)") { : argument is of length zero

此外:

> if (length(Type_Three == 0)
{Type_Three <- 0}
#This does nothing, even though the length is 0.

非常感谢任何帮助。

0 个答案:

没有答案