How to remove a specific pattern from a string in R?

user3664020

I have this string (for example).

str <- "T gwed is atyrt mtt yfdgfg grter effgf y"

I want to remove lone occurring alphabets from this string ('T' at the start and 'y' at the end in this case) and output should be

"gwed is atyrt mtt yfdgfg grter effgf"

I used this

str <- gsub("[A-Za-z] ", "", str)

But it gives this as a result.

[1] "gweiatyrmtyfdgfgrtey"

Here it considers cases like "gwed " also and hence it merges every word of the string.

How do i achieve my aim?

Also, I have this huge text with thousands of strings (not just a single string), so keep this in mind while providing an answer.

Henrik
str <- "T gwed is atyrt mtt yfdgfg grter effgf y"

gsub(" ?\\<[[:alpha:]]\\> ?", "", str)

## [1] "gwed is atyrt mtt yfdgfg grter effgf"

You need to use the special character to denote word boundaries, i.e., \\< and \\>. The _? (where _ is a space) denotes that you also want to remove single spaces around the single letters (if present). See ?regex for more.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove a specific pattern from a string in R?

From Dev

Remove specific pattern from string

From Dev

Remove specific pattern from string

From Dev

R: How to remove a string containing a specific character pattern?

From Dev

How to remove a specific pattern from a file

From Dev

Remove specified pattern from string in R

From Dev

Remove specified pattern from string in R

From Dev

How to remove a pattern from a string using Regex

From Dev

how to remove specific String from String in java

From Dev

How to remove extra spaces from a specific string?

From Dev

How to remove a specific string from WPF RichTextBox?

From Dev

How to remove specific part from a String in php

From Dev

How to remove line matching specific pattern from a file

From Dev

How to detect a string with specific pattern from a larger string?

From Dev

Pattern Remove from string php

From Dev

how to remove a multilined string/block of text pattern from a file?

From Dev

Remove pattern from database in R

From Dev

How to remove specific string from string using regex

From Dev

Need to remove some specific string from a column using R script

From Dev

How to split string to a specific pattern?

From Dev

SED: remove pattern from two specific lines

From Dev

SED: remove pattern from two specific lines

From Dev

bash : Remove the specific pattern of characters from a line

From Dev

How to remove + (plus sign) from string in R?

From Dev

How to remove \n and \r from a string

From Dev

How to remove single quote from a string in R?

From Dev

How to remove '\' from a string using R?

From Dev

Remove a string pattern and symbols from string

From Dev

Remove specific char from String

Related Related

HotTag

Archive