简体   繁体   English

从数字数据框中删除字符列

[英]remove character columns from a numeric data frame

I have a data frame like the one you see here. 我有一个像你在这里看到的数据框。

     DRSi       TP        DOC        DN          date     Turbidity   Anions 
     158        5.9       3371       264        14/8/06      5.83    2246.02
     217        4.7       2060       428        16/8/06      6.04    1632.29
     181        10.6      1828       219        16/8/06      6.11    1005.00
     397        5.3       1027       439        16/8/06      5.74    314.19
     2204       81.2      11770      1827       15/8/06      9.64    2635.39
     307        2.9       1954       589        15/8/06      6.12    2762.02
     136        7.1       2712       157        14/8/06      5.83    2049.86
     1502       15.3      4123       959        15/8/06      6.48    2648.12
     1113       1.5       819        195        17/8/06      5.83    804.42
     329        4.1       2264       434        16/8/06      6.19    2214.89
     193        3.5       5691       251        17/8/06      5.64    1299.25
     1152       3.5       2865       1075       15/8/06      5.66    2573.78
     357        4.1       5664       509        16/8/06      6.06    1982.08
     513        7.1       2485       586        15/8/06      6.24    2608.35
     1645       6.5       4878       208        17/8/06      5.96    969.32

Before I got here i used the following code to remove those columns that had no values at all or some NA's. 在我到达这里之前,我使用以下代码删除那些根本没有值或某些NA的列。

    rem = NULL
    for(col.nr in 1:dim(E.3)[2]){
      if(sum(is.na(E.3[, col.nr]) > 0 | all(is.na(E.3[,col.nr])))){
        rem = c(rem, col.nr)
      }
    }
    E.4 <- E.3[, -rem]  

Now I need to remove the "date" column but not based on its column name, rather based on the fact that it's a character string. 现在我需要删除“日期”列,但不是基于它的列名,而是基于它是一个字符串的事实。

I've seen here ( Remove an entire column from a data.frame in R ) already how to simply set it to NULL and other options but I want to use a different argument. 我在这里看到( 从R中的data.frame中删除整个列 )已经如何简单地将其设置为NULL和其他选项,但我想使用不同的参数。

First use is.character to find all columns with class character . 首先使用is.character查找具有类character所有列。 However, make sure that your date is really a character , not a Date or a factor . 但是,请确保您的日期确实是一个character ,而不是Datefactor Otherwise use is.Date or is.factor instead of is.character . 否则使用is.Dateis.factor而不是is.character

Then just subset the columns that are not characters in the data.frame, eg 然后,只需将data.frame中不是字符的列进行子集化,例如

df[, !sapply(df, is.character)]

I was having a similar problem but the answer above isn't resolve it for a Date columns (that's what I needed), so I've found another solution: 我遇到了类似的问题,但上面的答案并没有解决日期列(这就是我需要的),所以我找到了另一个解决方案:

df[,-grep ("Date|factor|character", sapply (df, class))]

Will return you your df without Date, character and factor columns. 如果没有日期,字符和因子列,将返回您的df。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM