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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.