【问题标题】:Split long string in three variables将长字符串拆分为三个变量
【发布时间】:2020-09-12 08:14:21
【问题描述】:

我有一个如下所示的数据框:

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) 第一部分由一些文本组成: 例如。在第 1 行“热离子、冷阴极和光阴极阀、管和零件”

2) 第二部分是大写字母: 例如。在第 1 行:“E”

3) 最后一部分是一个数字: 例如第 1 行是“16.3”。

我想拆分我的字符串并从中创建三个变量。 问题是每行的点数不同。 有谁知道如何有效地做到这一点?

隔离大写字母(第 2 部分)的有效方式就足够了。

非常感谢您的帮助

【问题讨论】:

    标签: r string split data-manipulation


    【解决方案1】:

    您可以使用正则表达式来查找长度为 2 或更多 {2,} 的点 [.]

    strsplit(df$string, "[.]{2,}")[1:3]
    # [[1]]
    # [1] " Thermionic, cold and photo-cathode valves, tubes, and parts "
    # [2] " E "                                                          
    # [3] " 16.3"                                                        
    # [[2]]
    # [1] " Automatic data processing machines and units thereof " " E "                                                   
    # [3] " 15.0"                                                 
    # [[3]]
    # [1] " Parts of and accessories suitable for 751, 752 " " E "                                             
    # [3] " 14.6"                                           
    

    有了这个,你可以将它转换成一个框架:

    data.frame(do.call(rbind, strsplit(df$string, "[.]{2,}")), stringsAsFactors = FALSE)
    #                                                              X1  X2    X3
    # 1  Thermionic, cold and photo-cathode valves, tubes, and parts   E   16.3
    # 2         Automatic data processing machines and units thereof   E   15.0
    # 3               Parts of and accessories suitable for 751, 752   E   14.6
    # 4                            Optical instruments and apparatus   E   14.1
    # 5                 Perfumery, cosmetics and toilet preparations   E   13.3
    # 6                                                         Silk   A   13.2
    # 7                          Undergarments, knitted or crocheted   B   13.1
    # 8               Articles of materials described in division 58   D   13.1
    

    您可能需要重命名 trimwsas.numeric 某些列,因为 strsplit 没有修剪字符串。

    如果你只需要第二列,那么

    trimws(sapply(strsplit(df$string, "[.]{2,}"), `[[`, 2))
    # [1] "E" "E" "E" "E" "E" "A" "B" "D"
    

    【讨论】:

    • 非常感谢您,这可以完成这项工作!只有两件事:是否可以在 df 中保留其他变量? idsh3。再次感谢我是字符串分析的新手!
    • 你的意思是df$middleletter &lt;- trimws(sapply(strsplit(df$string, "[.]{2,}"), `[[`, 2))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2013-12-03
    • 2020-12-27
    相关资源
    最近更新 更多