gsub replace and clean in same line of code

I was wondering if it is possible to replace a character and remove the blank space in one line .

string = c("av13 personal care", "-11", "av13 personal care", "av14 personal services", 
    "av15 meals", "av29 visit friends", "av17 free time travel", 
 "av27 pubs", "av28 restaurants", "av28 restaurants", "av29 visit friends", 
 "av37 conversation", "av14 personal services", "av13 personal care", 
 "av13 personal care", "av13 personal care", "-11", "av13 personal care", 
 "av13 personal care", "av15 meals", "av6 cook, wash up", "av40 other leisure", 
 "av37 conversation", "av21 walking", "av40 other leisure", "av15 meals", 
 "av6 cook, wash up", "av13 personal care", "av21 walking", "av17 free time travel", 
 "av15 meals", "av35 read papers, magazines", "av27 pubs", "av13 personal     care", 
   "-11", "av13 personal care", "av2 paidwork at home", "av25 dances or    parties", 
"av1 paid work", "av1 paid work", "av1 paid work", "av1 paid work", 
"av2 paidwork at home", "av2 paidwork at home", "av13 personal care", 
 "av17 free time travel", "av29 visit friends", "av17 free time travel", 
 "av13 personal care", "-11", "av13 personal care")

Instead of always doing this

clean = gsub(pattern = "[A-z]", replacement = "", x = string)
clean = gsub(pattern = "[[:blank:]]", replacement = "", x = clean)

Is it possible to insert [[:blank:]] directly in the first line ?

I also have a problem with "," ( "35," ), how can I get rid of it also in the first line ?

small update I realise that in my (huge) dataset I also get / after gsub my string - could you also help me to remove it ?


Here is a one-liner:

gsub(pattern = "[A-z ,/]", replacement = "", x = string) # added / to address the update

or

gsub(pattern = "[A-z]| |,|/", replacement = "", x = string)

As hwnd pointed out the range [Az] actually includes several special character that fall between AZ and az in Ascii code (relevant SO answer and the ASCII table). These special characters are: [ , , ] , ^ , _ , and `


The character class [Az] will match other characters as well, I would use:

gsub('[a-zA-Zt ,]', '', string)

Note: The POSIX bracketed expression [:blank:] matches the space and tab character.

If space is only necessary then:

gsub('[a-zA-Z, ]', '', string)
链接地址: http://www.djcxy.com/p/38318.html

上一篇: R gsub空白后的所有内容

下一篇: gsub用同一行代码替换和清理