R从字符串中删除最后一个字 [英] R remove last word from string

查看:1302
本文介绍了R从字符串中删除最后一个字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做某事但不记得/找到答案。我从人口普查局获得了一个城市名称列表,他们把城市的类型放在了结尾处,这使得我的 match()

I'm trying to do something but can't remember/find the answer. I have a list of city names from the Census Bureau and they put the city's type on the end which is messing up my match().

我想这样做:

Middletown Township
Sunny Valley Borough
Hillside Village

纳入:

into this:

Middletown
Sunny Valley
Hillside

有什么建议?理想情况下,我也想知道R中是否有 lastIndexOf()函数。

Any suggestions? Ideally I'd also like to know if there's a lastIndexOf() function in R.

这里是dput:

Here's the dput:

> dput(df1)
structure(list(id = c(1, 2, 3), city = structure(c(2L, 3L, 1L
), .Label = c("Hillside Village", "Middletown Township", "Sunny Valley Borough"
), class = "factor")), .Names = c("id", "city"), row.names = c(NA, 
-3L), class = "data.frame")


推荐答案

这会起作用:

This will work:

gsub("\\s*\\w*$", "", df1$city)
[1] "Middletown"   "Sunny Valley" "Hillside"   

它删除由一个或多个空格字符组成的任何子字符串,后跟任意数量的单词字符(空格,数字或下划线),然后是字符串的结尾。

It removes any substring consisting of one or more space chararacters, followed by any number of "word" characters (spaces, numbers, or underscores), followed by the end of the string.

这篇关于R从字符串中删除最后一个字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆