1
xcv(123)
wert(232)
t(145)
tyui ier(133)
ytie(435)
...
字符串的长度是动态的,意味着它是随机的。括号之间的数字是目标字母,需要在同一数据集的新列中存储的&。如何从字符变量中取出特定字母
以下关键词可以帮助: substr()
strsplit()
我积极地寻找一个答案。您的帮助将深受赞赏。
xcv(123)
wert(232)
t(145)
tyui ier(133)
ytie(435)
...
字符串的长度是动态的,意味着它是随机的。括号之间的数字是目标字母,需要在同一数据集的新列中存储的&。如何从字符变量中取出特定字母
以下关键词可以帮助: substr()
strsplit()
我积极地寻找一个答案。您的帮助将深受赞赏。
你的意思是你想从字符串xcv
中提取例如第123个字母?
set.seed(123)
xcv <- paste(sample(letters, 200, replace = TRUE), collapse = "")
n <- 123
您可以提取n
个字母,像这样:
substr(xcv, n, n)
# [1] "i"
dat = c('xcv(123)' ,'wert(232)', 't(145)', 'tyui ier(133)', 'ytie(435)')
target = gsub(".*\\(|\\).*", "", dat) #captures anything in between '(' and ')'. We use \\(and \\) to denote the brackets since they are special characters.
cbind(dat, target)
dat target
[1,] "xcv(123)" "123"
[2,] "wert(232)" "232"
[3,] "t(145)" "145"
[4,] "tyui ier(133)" "133"
[5,] "ytie(435)" "435"
你可以做'库(stringr); str_extract(v1,“(?<= \\()[0-9] +(?= \\))”)'或另一个选项是'library(readr); parse_number(v1)' – akrun
在您的最后一个字符串中,有'435',但你的字只有4个字母(假设我正确理解了这一点)。您应该添加您的预期输出。与第二个一样 – Sotos