To keep only alphabets in a string in R, you can use the 'gsub' function with a regular expression pattern to remove non-alphabetic characters.
In this example,
str1
with a value of "Hello123 World"
."[^[:alpha:]]"
to match non-alphabetic characters and replace them with an empty string.str1
.str1 <- "Hello123 World"
str1 <- gsub("[^[:alpha:]]", "", str1)
print(paste("Modified string:", str1))
[1] "Modified string: HelloWorld"
In this example,
str2
with a value of "apple@banana"
."[^[:alpha:]]"
to match non-alphabetic characters and replace them with an empty string.str2
.str2 <- "apple@banana"
str2 <- gsub("[^[:alpha:]]", "", str2)
print(paste("Modified string:", str2))
[1] "Modified string: applebanana"
In this tutorial, we learned How to keep only Alphabets in a String in R language with well detailed examples.