我想知道是否可以替换字符并删除一行中的空格。
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")
而不是总是这样做
clean = gsub(pattern = "[A-z]", replacement = "", x = string)
clean = gsub(pattern = "[[:blank:]]", replacement = "", x = clean)
是否可以将[[:blank:]]
直接插入第一行?
我对","
("35,"
)也有问题,如何在第一行中也消除它?
小更新我意识到,在我的(巨大)数据集中,我的gsub
和string
之后也得到了/
,你能帮我删除它吗?
这里有一行代码:
gsub(pattern = "[A-z ,/]", replacement = "", x = string) # added / to address the update
或
gsub(pattern = "[A-z]| |,|/", replacement = "", x = string)
正如hwnd所指出的,范围[A-z]
实际上包括Ascii代码中A-Z和A-Z之间的几个特殊字符(相关的SO答案和Ascii表)。这些特殊字符是:[
、、
]
、^
、_
和`
字符类[A-z]
也会匹配其他字符,我会使用:
gsub('[a-zA-Zt ,]', '', string)
注意:带括号的POSIX表达式[:blank:]
与空格和制表符匹配。
如果只需要空间,则:
gsub('[a-zA-Z, ]', '', string)