r - 如何获取最后一个逗号后的文本?
问题描述
我有一个文本,说:
s <- "Chengdu Shi, Sichuan Sheng, China"
我想使用一个函数,这样我只会得到最后一个逗号之后的单词(即中国)。
我尝试了几种方法,例如 grep,但它们返回所有实例而不是最后一个。
解决方案
使用sub
:
# Simply get the text after last comma ,
sub('.*\\,', '', s)
或者
word
从library(stringr)
包中使用:
s <- "Chengdu Shi, Sichuan Sheng, China"
word(s,3,sep=",") # Extract word from the last column
或者
# If your data is stored in data.frame
word(s,ncol(s),sep=",") # Extract last column data using column index
输出:
[1] " China"
推荐阅读
- java - Java中引用变量的枚举
- r - 单细胞实验。为什么我在单细胞实验中没有成功更改行名和列名?
- javascript - 如何从 Cloud Firestore 数据库中检索所有文档(包含图像 url 和名称)到网站?
- python - 如何在没有空格的文本列中查找所有文本
- excel - 获取“运行时错误'1004':范围类的选择方法失败”
- c++ - 为什么 moc 错误不是由纯方法引起的?
- cakephp - CakePHP 3.x 关于订单的 case 语句
- java - java.io.FileNotFoundException:打开失败:EISDIR(是一个目录)
- python - 输入在 VS Code 中可用?
- server - 带有 BI 应用程序的服务器每天在固定时间停机