4

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.

    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.

Community
  • 1
  • 1
umbra
  • 111
  • 1
  • 1
  • 10

2 Answers2

15

First use is.character to find all columns with class character. However, make sure that your date is really a character, not a Date or a factor. Otherwise use is.Date or is.factor instead of is.character.

Then just subset the columns that are not characters in the data.frame, e.g.

df[, !sapply(df, is.character)]
shadow
  • 21,823
  • 4
  • 63
  • 77
  • 5
    Or, a destructive approach: `df[sapply(df, is.character)] <- list(NULL)`. – A5C1D2H2I1M1N2O1R2T1 Jul 25 '13 at 16:08
  • @shadow thanks for the tip about checking first if it's a character, factor or Date. It was in fact a factor so I switched is.character with is.factor and worked perfectly. Ananda Matho your example worked just as well, thank you! – umbra Jul 26 '13 at 10:02
  • @umbra, do consider marking shadow's answer as *accepted* if it solves your problem. Thanks. – A5C1D2H2I1M1N2O1R2T1 Jul 26 '13 at 16:30
  • Where can one find this interesting 'is.Date' function? – GWD Nov 08 '13 at 18:50
  • @WD_R: Apparently it does not exist... (Or at least I can't find it anymore.) You could define `is.Date <- function(d) "Date" %in% class(d)`, but that only finds dates of class `Date` and will not e.g. find `POSIXct` dates. – shadow Nov 11 '13 at 14:56
4

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.

Andriy T.
  • 2,020
  • 12
  • 23