2013-04-17 35 views
-2

我有这个数据帧。我想在数据框中只有数字。把它清理干净,这样就没有文字,没有%,没有 - 。我尝试过使用gsub,但是当我使用sub时,它将我的数据框转换为字符,并且无法再将它转换回数据框。任何想法如何摆脱我的数据框中的字符和破折号?我也需要去掉emtpy行。解析出数据帧中的文本和非数字字符

V1 V2  V3  V4 V5 V6  V7 V8 V9 V10 
1 %user %sys %wait %idle physc %entc lbusy app vcsw phint 
2 ----- ----- ------ ------ ----- ----- ------ --- ----- ----- 
3 36.4 13.1 13.9 36.6 9.26 57.9 28.0 34.96 26049 3492 
4 31.1 11.2 12.6 45.1 7.81 48.8 25.9 37.85 17515 2754 
5 33.2 13.4 13.2 40.3 8.69 54.3 26.9 35.67 23510 3265 
6 34.0 12.8 13.7 39.4 8.77 54.8 26.5 35.19 25151 3305 
7 32.7 12.4 13.6 41.3 8.49 53.0 25.9 35.97 25214 3201 
8 33.4 13.7 12.5 40.3 8.76 54.7 27.1 36.50 23943 3391 
4 %user %sys %wait %idle physc %entc lbusy app vcsw phint 
25 ----- ----- ------ ------ ----- ----- ------ --- ----- ----- 
26 32.9 14.1 11.3 41.7 8.66 54.1 27.9 36.46 22438 3253 
27 33.2 13.9 12.0 41.0 8.74 54.6 27.4 37.38 23838 3135 
28 30.5 13.3 11.0 45.1 8.13 50.8 26.2 37.42 21912 2752 
29 29.9 13.4 11.8 44.8 8.11 50.7 25.5 37.92 23030 2791 
30 30.6 12.6 11.1 45.8 8.01 50.1 25.7 37.03 21844 2811 
31 32.6 12.2 11.4 43.8 8.30 51.9 28.0 36.84 22227 2723 

这不起作用:

GSUB( “ - ”, “”,XX)

+1

怎么样'从'plyr'包colwise'。然后你可以写下如下的内容:'xx < - na.omit(colwise(as.numeric)(xx))'。或者只是摆脱前两行:'xx [-1:2,]' – Justin

+0

您是否想要删除标题和第1行? –

回答

0

只需卸下data.frame的前两行:

data1 <- data1[-(1:2),] 

添加评论。看来你想删除的行总是以“%user”或“-----”开头。你可以像这样删除它们。这是未经测试的代码,因为您没有提供可重复的示例。

data1[!(data1[,1]=="%user"|data1[,1]=="-----"),] 
+0

@ P Lapointe,这是行不通的,因为我在midle和数据框的其他地方都有角色。 – user1471980

+0

你能举一个你的data.frame的好例子吗?你目前没有特殊字符。 –

+0

我已更新原始帖子。 – user1471980

3

我会做的是试图将所有对象转换成numeric

numerics_only <- apply(data, 2, as.numeric) 

试图迫使文本数字将介绍NA S:

Warning messages: 
1: In apply(data, 2, as.numeric) : NAs introduced by coercion 

这些,你可以,如果你抑制喜欢。然后过滤出所有具有NAs的行。

newdata <- na.omit(numerics_only) 

沿线的东西。

0

只是为了表明,随着课程的as.numeric()的方法的工作原理: (适用于您的例子复制到一个文本文件)

> df <- read.delim("testdf.txt", sep = " ", as.is = T)[,-1] 
> str(df) 
'data.frame': 16 obs. of 10 variables: 
$ V1 : chr "%user" "-----" "36.4" "31.1" ... 
$ V2 : chr "%sys" "-----" "13.1" "11.2" ... 
$ V3 : chr "%wait" "------" "13.9" "12.6" ... 
$ V4 : chr "%idle" "------" "36.6" "45.1" ... 
$ V5 : chr "physc" "-----" "9.26" "7.81" ... 
$ V6 : chr "%entc" "-----" "57.9" "48.8" ... 
$ V7 : chr "lbusy" "------" "28.0" "25.9" ... 
$ V8 : chr "app" "---" "34.96" "37.85" ... 
$ V9 : chr "vcsw" "-----" "26049" "17515" ... 
$ V10: chr "phint" "-----" "3492" "2754" ... 
> (df <- data.frame(na.omit(apply(df, 2, as.numeric)))) 
    V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 
1 36.4 13.1 13.9 36.6 9.26 57.9 28.0 34.96 26049 3492 
2 31.1 11.2 12.6 45.1 7.81 48.8 25.9 37.85 17515 2754 
3 33.2 13.4 13.2 40.3 8.69 54.3 26.9 35.67 23510 3265 
4 34.0 12.8 13.7 39.4 8.77 54.8 26.5 35.19 25151 3305 
5 32.7 12.4 13.6 41.3 8.49 53.0 25.9 35.97 25214 3201 
6 33.4 13.7 12.5 40.3 8.76 54.7 27.1 36.50 23943 3391 
7 32.9 14.1 11.3 41.7 8.66 54.1 27.9 36.46 22438 3253 
8 33.2 13.9 12.0 41.0 8.74 54.6 27.4 37.38 23838 3135 
9 30.5 13.3 11.0 45.1 8.13 50.8 26.2 37.42 21912 2752 
10 29.9 13.4 11.8 44.8 8.11 50.7 25.5 37.92 23030 2791 
11 30.6 12.6 11.1 45.8 8.01 50.1 25.7 37.03 21844 2811 
12 32.6 12.2 11.4 43.8 8.30 51.9 28.0 36.84 22227 2723 
Warnmeldungen: 
1: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
2: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
3: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
4: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
5: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
6: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
7: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
8: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
9: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt 
10: In apply(df, 2, as.numeric) : NAs durch Umwandlung erzeugt