在 R 中将数字转换为字符串,同时保持字符不变

Wor*_*rse 1 r

我有一个如下所示的向量:

> vector <- c("1","2","0", "10", "name", "hello")
Run Code Online (Sandbox Code Playgroud)

我想将其转换为每个数字字符变成一个字符串,而不影响实际的字符串。

[1] "test" "test" "test" "test" "name" "hello"
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Ony*_*mbu 5

sub("^\\d+$", "test", vector)
[1] "test"  "test"  "test"  "test"  "name"  "hello"

str_replace(vector, "^[0-9]+$", "test")
[1] "test"  "test"  "test"  "test"  "name"  "hello"
Run Code Online (Sandbox Code Playgroud)