[英]Removing characters in a string with R
How can I delete rows in a DF that have letters on it when they are supposed to be numbers? 当它们应该是数字时,如何删除DF中包含字母的行? 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 check_pattern(pattern,string)出错:字符串和模式的长度不兼容
Use grepl
使用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
?? 但是,实际上,你有一个data.table
对象,为什么data.frame
它转换为data.frame
? That doesn't make any sense to me. 这对我没有任何意义。 You can do the same within your original data.table
您可以在原始data.table
执行相同的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
或者使用grep
与invert = TRUE
结合使用
DT[grep("[A-Za-z]", j, invert = TRUE), ]
Or if you want to use str_detect
(like in your post) 或者如果你想使用str_detect
(就像在帖子中一样)
library(stringr)
DT[!str_detect(j, "[A-Za-z]"), ]
Although str_detect
is just a wrapper for grepl
虽然str_detect
仅仅是一个包装grepl
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.