2

我正在构建一个使用闪亮和露天的应用程序来分析风数据。
现在需要在用户上传数据之前“清理”数据。我有兴趣自动执行此操作。有些数据是空的,有些不是数字,所以不可能建立一个风玫瑰图。我想要:

    1. 估计有多少数据不是数字
    2. 剪掉它,只留下数字数据

这是数据的一个示例:
“NO2.mg”被读取为一个因子而不是 int,因为它不只包含数字
OK
这是一个可重复的示例:

no2<-factor(c(5,4,"c1",54,"c5",seq(2:50)))
no2
[1] 5  4  c1 54 c5 1  2  3  4  5  6  7  8  9  10 11 12 13 14
[20] 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
[39] 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
52 Levels: 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 ... c5
> as.numeric(no2)
[1] 45 34 51 46 52  1 12 23 34 45 47 48 49 50  2  3  4  5  6
[20]  7  8  9 10 11 13 14 15 16 17 18 19 20 21 22 24 25 26 27
[39] 28 29 30 31 32 33 35 36 37 38 39 40 41 42 43 44
4

3 回答 3

9

最糟糕的 R 俳句:

Some of the data is empty, 
some of is not numeric, 
so it is not possible to build a wind rose.
于 2013-08-07T06:21:44.480 回答
4

要将因子转换为数字,您需要先转换为字符:

no2<-factor(c(5,4,"c1",54,"c5",seq(2:50)))
no2_num <- as.numeric(as.character(no2)) 
#Warning message:
#  NAs introduced by coercion 
no2_clean <- na.omit(no2_num) #remove NAs resulting from the bad data

# [1]  5  4 54  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# [40] 37 38 39 40 41 42 43 44 45 46 47 48 49
# attr(,"na.action")
# [1] 3 5
# attr(,"class")
# [1] "omit"

length(attr(no2_clean,"na.action"))/length(no2)*100
#[1] 3.703704
于 2013-08-07T07:18:54.920 回答
1

好的,我就是
这样

no2<-factor(c(5,4,"c1",54,"c5",seq(2:50)))

sum(is.na((as.numeric(as.vector(no2)))))

并估计不良数据的百分比:
sum(is.na((as.numeric(as.vector(no2)))))/length(no2)*100

于 2013-08-07T07:01:38.740 回答