R 从 str_split 获取最后一个元素 [英] R get last element from str_split
本文介绍了R 从 str_split 获取最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 R 字符串列表,我想获取每个字符串的最后一个元素
I have a R list of strings and I want to get the last element of each
require(stringr)
string_thing <- "I_AM_STRING"
Split <- str_split(string_thing, "_")
Split[[1]][length(Split[[1]])]
但是我如何使用字符串列表来做到这一点?
but how can I do this with a list of strings?
require(stringr)
string_thing <- c("I_AM_STRING", "I_AM_ALSO_STRING_THING")
Split <- str_split(string_thing, "_")
#desired result
answer <- c("STRING", "THING")
谢谢
推荐答案
正如对您问题的评论所暗示的,这适用于 gsub
:
As the comment on your question suggests, this is suitable for gsub
:
gsub("^.*_", "", string_thing)
我建议您同时注意以下情况.
I'd recommend you take note of the following cases as well.
string_thing <- c("I_AM_STRING", "I_AM_ALSO_STRING_THING", "AM I ONE", "STRING_")
gsub("^.*_", "", string_thing)
[1] "STRING" "THING" "AM I ONE" ""
这篇关于R 从 str_split 获取最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文