【问题标题】:Extracting data frame from strings between values in R从R中值之间的字符串中提取数据框
【发布时间】:2020-03-01 20:46:49
【问题描述】:

我正在R 中进行一些编码,并且我正在处理一些数据帧被编码为单个字符串的情况,并且必须能够从字符串中恢复数据帧。字符串形式如下:

|Carrot^14|Cucumber^3|Potato^8|

分隔符| 分隔数据框的行,分隔符^ 分隔具有字符值的列和具有数值的列。 (为简化问题,请注意这些符号仅作为分隔符出现 --- 它们从未出现在数据框中的值中。)与此字符串对应的数据框将是:

 Vegetable  Quantity
  "Carrot"        14
"Cucumber"         3
  "Potato"         8

我希望能够解码一个字符串以提取它编码的数据帧。我知道这需要某种regexp 查询,但我不太确定该怎么做。我也不确定数据框和单个字符串之间的这种对应关系是否有任何特定名称可以引导我了解有关该技术的更多信息。

我的问题: 这种从数据框到单个字符串(以及返回)的编码是否具有特定名称?如何解码R中的字符串?

【问题讨论】:

    标签: r string


    【解决方案1】:

    您可以将竖线换成换行符,然后使用read.table()

    txt <- "|Carrot^14|Cucumber^3|Potato^8|"
    
    read.table(text = gsub("\\|", "\n", txt), sep = "^", col.names = c("Vegetable", "Quantity"))
    
      Vegetable Quantity
    1    Carrot       14
    2  Cucumber        3
    3    Potato        8
    

    【讨论】:

      【解决方案2】:

      我们可以使用gsub删除字符串开头和结尾的分隔符,根据(|)分隔符将数据放在单独的行中,然后根据(^)分隔符将数据放入单独的列中。

      library(dplyr)
      library(tidyr)
      
      df %>%
        mutate(col = gsub('^\\||\\|$', '' ,col)) %>%
        separate_rows(col, sep = "\\|") %>%
        separate(col, into = c('Vegetable', 'Quantity'), sep = "\\^", convert = TRUE)
      
      #  Vegetable Quantity
      #1    Carrot       14
      #2  Cucumber        3
      #3    Potato        8
      

      数据

      df <- data.frame(col = "|Carrot^14|Cucumber^3|Potato^8|", stringsAsFactors = FALSE)
      

      【讨论】:

        【解决方案3】:

        一种方法是直接提取蔬菜名称和编号。对于蔬菜,我提取了介于|^ 之间的字符。对于数字,我只是提取了数字字母。我将它们转换为数字。

        library(stringi)
        
        data.frame(vegetable = unlist(stri_extract_all_regex(str = foo$whatever,
                                              pattern = "(?<=\\|)[A-z]+(?=\\^)")),
                   quantity = as.numeric(unlist(stri_extract_all_regex(str = foo$whatever, pattern = "[0-9]+")))
                   )
        
          vegetable quantity
        1    Carrot       14
        2  Cucumber        3
        3    Potato        8
        4    Carrot       20
        5  Cucumber        5
        6    Potato       12
        

        数据

        foo <- structure(list(whatever = c("|Carrot^14|Cucumber^3|Potato^8|", 
        "|Carrot^20|Cucumber^5|Potato^12|")), row.names = c(NA, -2L), class = c("tbl_df", 
        "tbl", "data.frame"))
        

        【讨论】:

          【解决方案4】:

          您可以尝试以下基本 R 解决方案

          df <- setNames(data.frame(do.call(rbind,
                             strsplit(unlist(regmatches(s,gregexpr("(?<=\\|).*?(?=\\|)",s,perl = TRUE))),
                                      "\\^"))),
                            c("Vegatable","Quantity"))
          df$Quantity <- as.numeric(df$Quantity)
          

          这样

          > df
            Vegatable Quantity
          1    Carrot        1
          2  Cucumber        2
          3    Potato        3
          

          数据

          s <- "|Carrot^14|Cucumber^3|Potato^8|"
          

          【讨论】:

            猜你喜欢
            • 2021-12-24
            • 2022-11-02
            • 1970-01-01
            • 2020-06-07
            • 2022-01-17
            • 2014-03-10
            • 2015-05-31
            • 2023-02-14
            • 2021-12-03
            相关资源
            最近更新 更多