【发布时间】:2016-06-29 15:32:54
【问题描述】:
我有一个带有变量 (care_group) 的 data.frame example,如下所示:
> example
care_group
1 1st Choice Care Homes 8.8
2 2Care
3 229 Mitcham Lane Ltd
4 3 L Care Ltd
5 3AB Care Ltd
6 9Grace Road Ltd
7 A&R Care Ltd 9.7
8 ABLE (Action for a Better Life)
9 A C L Care Homes Ltd
10 A D L plc
11 A D R Care Homes Ltd
12 A G E Nursing Homes Ltd 8
您可能会注意到,我的一些观察结果是字母数字,并且在开头和/或结尾名称中都包含数字。我知道可以摆脱数字字符(例如参见here)。然而,我不知道如何只删除其中的一些。具体来说,删除名称末尾的数字并保留开头的数字。我试图通过创建一个包含我要删除的数字的组并尝试使用gsub 来做到这一点。
ratings = c("8", "8.8", "9.7")
example$new_var = with(example, gsub(ratings, " ", care_group))
但是我收到此警告消息:
Warning message:
In gsub(ratings, " ", care_group) :
argument 'pattern' has length > 1 and only the first element will be used
我想知道是否可以将 gsub 与长度 > 1 的模式一起使用,或者是否有人可以提出一种更有效的方法来解决这个问题。提前谢谢了。
【问题讨论】: