R:从字符向量中删除数字

时间:2014-09-30 11:59:51

标签: r numbers character

我在R中获得了下一个向量:

 vec <- c("hello","3","-52","b2b","1j","place") 

现在我要删除&#34; 3&#34;和&#34; -52&#34;,但我不想删除&#34; b2b&#34;和&#34; 1j&#34;。

我可以在代码中添加什么来删除像&#34; 40&#34;在以下向量中:

vec2 <- c("hello","3","-52","b2b","1j","place 40") 

1 个答案:

答案 0 :(得分:0)

假设你的载体是:

 vec1 <- c("hello", "3", "-52", "b25b", "+22", "1j5", "place", "32.50")
 gsub("^[-+]?[0-9.]+$", " ", vec1)
 #[1] "hello" " "     " "     "b25b"  " "     "1j5"   "place" " " 

也适用于vec

 gsub("^[-+]?[0-9.]+$", " ", vec)
 #[1] "hello" " "     " "     "b2b"   "1j"    "place"