3

How can I delete rows in a DF that have letters on it when they are supposed to be numbers? A table example might be:

DT = data.table(x=c("b","b","b","a","a"),v=rnorm(5), j=c("122","1223","g21bg","43","534"))
DF=data.frame(DT)

And I need to get:

  x          v     j
 b  0.4220836   122
 b -1.9492471  1223
 a  1.4615694    43
 a -0.2294917   534

Could be any character non numeric. I tried

library(stringr)
str_detect(DF$j, letters)

But I get:

Error in check_pattern(pattern, string) : Lengths of string and pattern not compatible

APerson
  • 8,140
  • 8
  • 35
  • 49
GabyLP
  • 3,649
  • 7
  • 45
  • 66
  • A reproducible example would be great. Check this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Henk Sep 11 '14 at 14:00

1 Answers1

5

Use grepl

DF[!grepl("[A-Za-z]", DF$j), ]
##  x          v    j
##1 b -1.3157423  122
##2 b -1.3514456 1223
##4 a  0.7508370   43
##5 a  0.3476453  534

But, really, you have a data.table object, why are you converting it to a data.frame?? That doesn't make any sense to me. You can do the same within your original data.table

DT[!grepl("[A-Za-z]", j), ]
#    x           v    j
# 1: b  0.03008628  122
# 2: b -0.72063192 1223
# 3: a  0.94851720   43
# 4: a -0.72384496  534

Or using grep combined with invert = TRUE

DT[grep("[A-Za-z]", j, invert = TRUE), ]

Or if you want to use str_detect (like in your post)

library(stringr)
DT[!str_detect(j, "[A-Za-z]"), ]

Although str_detect is just a wrapper for grepl

David Arenburg
  • 91,361
  • 17
  • 137
  • 196