【问题标题】:add row using each word in a string that is separated by underscore使用下划线分隔的字符串中的每个单词添加行
【发布时间】:2021-05-08 00:53:38
【问题描述】:

我有一个很长的字符串向量,每个字符串由五个子字符串组成,每个字符串用下划线符号分隔:

例如,这里是字符串向量中的两个元素:

"land_somewhat_crop_produce_b.tif"
"marine_something_fish_meat_a.tif"

我想创建一个由这些子字符串组成的数据框。

col1 col2 col3 col4 col5
land somewhat crop produce b
marine something fish meat a

使用正则表达式模式匹配,我如何提取每个下划线之间的每个子字符串并使用这些子字符串为每一行创建一个数据框?

【问题讨论】:

  • 你可以做data.frame(orig=c("land_somewhat_crop_produce_b.tif", "marine_something_fish_meat_a.tif")) %>% tidyr::separate(orig, into=paste0("col", 1:5))

标签: r regex dplyr tidyr stringr


【解决方案1】:

你也可以这样做:

read.table(text = sub("\\.tif$", "", data), sep = "_")

      V1        V2   V3      V4 V5
1   land  somewhat crop produce  b
2 marine something fish    meat  a

【讨论】:

    【解决方案2】:
    data <- gsub(".tif","",data)
    data.frame(do.call(rbind,strsplit(data,"_")))
    

    给予,

          X1        X2   X3      X4 X5
    1   land  somewhat crop produce  b
    2 marine something fish    meat  a
    

    数据:

    data <- c("land_somewhat_crop_produce_b.tif","marine_something_fish_meat_a.tif")
    

    【讨论】:

      【解决方案3】:

      你可以从splitstackshape 使用cSplit

      data <- data.frame(col = sub('\\.tif$', '', data))
      splitstackshape::cSplit(data, 'col', sep = '_')
      
      #    col_1     col_2 col_3   col_4 col_5
      #1:   land  somewhat  crop produce     b
      #2: marine something  fish    meat     a
      

      【讨论】:

        猜你喜欢
        • 2019-01-23
        • 2018-09-28
        • 2013-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        • 2011-11-26
        相关资源
        最近更新 更多