当每个观察的第一个单词(相同的开头字符串模式)在 R 中包含不同的结尾字符串时,如何分隔连接的字符串?
How to separate a concatenated string when first word (same beginning string pattern) of every observation contains a different ending string in R?
在 R 中,我有一个更大的数据集,其中包含我需要解决的问题。所以我在 R 中有一个数据框,Post
变量中每个观察的第一个单词都有一个连接的字符串。幸运的是,字符串的开头包含相同的单词,但连接字符串的结尾总是不同的。有谁知道可以将 Introduction
与其连接(附加)到一串单词的单词分开的函数吗?换句话说,当每个观察的第一个单词(“Introduction
”相同的起始字符串模式)在 R 中包含不同的结尾字符串时,如何分离连接的字符串?
更新:完整且可重现的问题
dat <- data.frame(author=c("a", "b", "c", "d", "a", "b", "c", "d", "e", "a", "a", "a","a", "a", "c","c","c","c"),Post=c("Introductiontwo text", "IntroductionYoua need Introduction to give a complete and reproducible questionone text", "IntroductionYouas need Introduction to give a complete and reproducible questionthre text", "IntroductionYouasd need Introduction to give a complete and reproducible questionnice text", "IntroductionYouasds need Introduction to give a complete and reproducible questionwow text", "IntroductionYouasdsh need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshs need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsas need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text"))
dat
author Post
1 a Introductiontwo text
2 b IntroductionYoua need Introduction to give a complete and reproducible questionone text
3 c IntroductionYouas need Introduction to give a complete and reproducible questionthre text
4 d IntroductionYouasd need Introduction to give a complete and reproducible questionnice text
5 a IntroductionYouasds need Introduction to give a complete and reproducible questionwow text
6 b IntroductionYouasdsh need Introduction to give a complete and reproducible questionone text
7 c IntroductionYouasdshs need Introduction to give a complete and reproducible questionone text
8 d IntroductionYouasdshsa need Introduction to give a complete and reproducible questionone text
9 e IntroductionYouasdshsas need Introduction to give a complete and reproducible questionone text
10 a IntroductionYouasdshsasa need Introduction to give a complete and reproducible questionone text
11 a IntroductionYouasdshsasaa need Introduction to give a complete and reproducible questionone text
12 a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
13 a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
14 a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
15 c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
16 c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
17 c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
18 c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
您可以将 gsub
与捕获组一起使用
gsub("(Introduction)(.+)","\1 \2", dat$Post)
圆括号捕获 "Introduction" 和后面的字符。然后我们用匹配的值替换它们,中间有 space。
在 R 中,我有一个更大的数据集,其中包含我需要解决的问题。所以我在 R 中有一个数据框,Post
变量中每个观察的第一个单词都有一个连接的字符串。幸运的是,字符串的开头包含相同的单词,但连接字符串的结尾总是不同的。有谁知道可以将 Introduction
与其连接(附加)到一串单词的单词分开的函数吗?换句话说,当每个观察的第一个单词(“Introduction
”相同的起始字符串模式)在 R 中包含不同的结尾字符串时,如何分离连接的字符串?
更新:完整且可重现的问题
dat <- data.frame(author=c("a", "b", "c", "d", "a", "b", "c", "d", "e", "a", "a", "a","a", "a", "c","c","c","c"),Post=c("Introductiontwo text", "IntroductionYoua need Introduction to give a complete and reproducible questionone text", "IntroductionYouas need Introduction to give a complete and reproducible questionthre text", "IntroductionYouasd need Introduction to give a complete and reproducible questionnice text", "IntroductionYouasds need Introduction to give a complete and reproducible questionwow text", "IntroductionYouasdsh need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshs need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsas need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text","IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text", "IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text"))
dat
author Post
1 a Introductiontwo text
2 b IntroductionYoua need Introduction to give a complete and reproducible questionone text
3 c IntroductionYouas need Introduction to give a complete and reproducible questionthre text
4 d IntroductionYouasd need Introduction to give a complete and reproducible questionnice text
5 a IntroductionYouasds need Introduction to give a complete and reproducible questionwow text
6 b IntroductionYouasdsh need Introduction to give a complete and reproducible questionone text
7 c IntroductionYouasdshs need Introduction to give a complete and reproducible questionone text
8 d IntroductionYouasdshsa need Introduction to give a complete and reproducible questionone text
9 e IntroductionYouasdshsas need Introduction to give a complete and reproducible questionone text
10 a IntroductionYouasdshsasa need Introduction to give a complete and reproducible questionone text
11 a IntroductionYouasdshsasaa need Introduction to give a complete and reproducible questionone text
12 a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
13 a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
14 a IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
15 c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
16 c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
17 c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
18 c IntroductionYouasdshsasaaa need Introduction to give a complete and reproducible questionone text
您可以将 gsub
与捕获组一起使用
gsub("(Introduction)(.+)","\1 \2", dat$Post)
圆括号捕获 "Introduction" 和后面的字符。然后我们用匹配的值替换它们,中间有 space。