Removing characters in a string with R
How can I delete lines in DF that have letters on them when they should be numbers? An example table could 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
Can be any non-numeric character. I tried
library(stringr)
str_detect(DF$j, letters)
But I am getting:
Error in check_pattern (pattern, string): string length and pattern are incompatible
source to share
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 you really have an object data.table
, why are you converting it to data.frame
? It doesn't make any sense to me. You can do the same in your originaldata.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
in combination withinvert = TRUE
DT[grep("[A-Za-z]", j, invert = TRUE), ]
Or if you want to use str_detect
(as in your post)
library(stringr)
DT[!str_detect(j, "[A-Za-z]"), ]
Although it str_detect
is just a wrapper forgrepl
source to share