我有一个数据框,看起来像这样:
df<-structure(list(string = c(" Thermionic, cold and photo-cathode valves, tubes, and parts .................................. E ....................... 16.3",
" Automatic data processing machines and units thereof ............................................ E ....................... 15.0",
" Parts of and accessories suitable for 751, 752 .......................................................... E ....................... 14.6",
" Optical instruments and apparatus .............................................................................. E ....................... 14.1",
" Perfumery, cosmetics and toilet preparations ............................................................. E ....................... 13.3",
" Silk .................................................................................................................................. A ....................... 13.2",
" Undergarments, knitted or crocheted .......................................................................... B ....................... 13.1",
" Articles of materials described in division 58 ............................................................. D ....................... 13.1"
), id = c("1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 "), SH3 = c("776 ",
"752 ", "759 ", "871 ", "553 ", "261 ", "846 ", "893 ")), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
# that looks like this
string id SH3
<chr> <chr> <chr>
1 " Thermionic, cold and photo-cathode valves, tubes, and parts .................................. E ....................... 16.3" "1 " "776 "
2 " Automatic data processing machines and units thereof ............................................ E ....................... 15.0" "2 " "752 "
3 " Parts of and accessories suitable for 751, 752 .......................................................... E ....................... 14.6" "3 " "759 "
4 " Optical instruments and apparatus .............................................................................. E ....................... 14.1" "4 " "871 "
5 " Perfumery, cosmetics and toilet preparations ............................................................. E ....................... 13.3" "5 " "553 "
6 " Silk .................................................................................................................................. A ....................... 13.2" "6 " "261 "
7 " Undergarments, knitted or crocheted .......................................................................... B ....................... 13.1" "7 " "846 "
8 " Articles of materials described in division 58 ............................................................. D ....................... 13.1" "8 " "893 "
我想把
string
变量分成三个独立的变量。这个string
由一系列点(...)隔开的3部分组成。1) 第一部分由文本组成: 例如第一行中的 "Thermionic, cold and photo-cathode valves, tubes, and parts"
2) 第二部分是一个大写字母: 例如第一行中的 "E"
3) 最后一部分是一个数字: 例如第一行中的 "16.3"
我希望将字符串拆分并创建三个变量。问题在于每行点的数量都不同。 有没有人知道如何高效地完成这项任务?
如果能有效地分离大写字母(第2部分),就足以满足我的需求了。
非常感谢您提前的帮助。
id
和sh3
。再次感谢,我对字符串分析不熟悉! - Alexdf$middleletter <- trimws(sapply(strsplit(df$string, "[.]{2,}"), \
[[`, 2))` 吗? - r2evans