我有一个包含数字,字符和符号的数据集,我试过了
grep("[[:digit:]][[:punct:]]", input, value=T)
但是结果是:
[1] "0.05" "0.001" "0.01" "bb2+" "0.5" "0.1" "0.0001"
[8] "1.5" "2.5" "1.0" "aa(2+)" "0.2" "0.02" "0.3"
[15] "2." "1." "0.4" "3." "1.2" "0.03" "2.0"
[22] "10-AB" "0.6" "0.8" "3.5" "0.9" "WK4+" "5'"
[29] "0.005" "1.3" "1.6" "0.04" "1.4" "5-year" "1.8"
[36] "0.7" "1.1" "4." "1.7" "4.5" "3.0" "3'"
,您仍然可以看到带有字母字符结果的结果。想知道是否有更好的方法来仅将{/ {1}}字符与数字/浮点数/符号一起使用。
非常感谢您的答复!
抱歉,尚未在此问题的先前版本中提供预期的结果和输入。我对其进行了如下编辑:
输入:
grep()
预期输出:
input <- c("0.05","0.001","0.01","bb2+","0.5","0.1","0.0001", "1.5","2.5","1.0","aa(2+)","0.2","0.02","0.3"
,"2.","1.","0.4","3.","1.2","0.03","2.0"
,"10-AB","0.6","0.8","3.5","0.9","WK4+","5'"
,"0.005","1.3","1.6","0.04","1.4","5-year","1.8"
,"0.7","1.1","4.","1.7","4.5","3.0","3'")
答案 0 :(得分:1)
一种方法是排除所有具有任何字母字符的元素。使用grep
,我们可以使用invert = TRUE
来获取不包含任何字母的元素。
grep("[[:alpha:]]", input, value = TRUE, invert = TRUE)
# [1] "0.05" "0.001" "0.01" "0.5" "0.1" "0.0001" "1.5" "2.5"
# [9] "1.0" "0.2" "0.02" "0.3" "2." "1." "0.4" "3."
#[17] "1.2" "0.03" "2.0" "0.6" "0.8" "3.5" "0.9" "5'"
#[25] "0.005" "1.3" "1.6" "0.04" "1.4" "1.8" "0.7" "1.1"
#[33] "4." "1.7" "4.5" "3.0" "3'"