【问题标题】:R split array into Data frameR将数组拆分为数据框
【发布时间】:2018-07-28 16:19:54
【问题描述】:

对 R 非常陌生,并且很难确切知道要问什么,在这里发现了一个类似的问题 How to split a character vector into data frame? 但这有固定的长度,我无法针对我的问题进行调整

我在 R 的数组中有一些数据

TEST <- c("Value01:100|Value02:200|Value03:300|","Value04:1|Value05:2|",
            "StillAValueButNamesAreNotConsistent:12345.6789|",
              "AlsoNotAllLinesAreTheSameLength:1|")

数据成对存储,我希望像这样拆分成一个数据框:

Variable Value
Value01    100
Value02    200
Value03    300
Value04    1
Value05    2
StillAValueButNamesAreNotConsistent   12345.6789
AlsoNotAllLinesAreTheSameLength     1

变量名是一个字符串,值总是一个数字

任何帮助都会很棒!

谢谢

【问题讨论】:

    标签: r


    【解决方案1】:

    可以使用基于tidyr 的解决方案。将向量 TEST 转换为 data.frame 并从每一行中删除最后一个 |,因为这没有任何意义。

    现在,使用tidyr::separate_rows 扩展基于| 的行,然后使用tidyr::separate 函数将数据分成2 列。

    library(dplyr)
    library(tidyr)
    
    data.frame(TEST) %>%
      mutate(TEST = gsub("\\|$","",TEST)) %>%
      separate_rows(TEST, sep = "[|]") %>%
      separate(TEST, c("Variable", "Value"), ":")
    
    #                              Variable      Value
    # 1                             Value01        100
    # 2                             Value02        200
    # 3                             Value03        300
    # 4                             Value04          1
    # 5                             Value05          2
    # 6 StillAValueButNamesAreNotConsistent 12345.6789
    # 7     AlsoNotAllLinesAreTheSameLength          1
    

    【讨论】:

      【解决方案2】:

      我们可以在base R中用一行来完成。只需将| 字符更改为换行符,然后使用: 作为sep 中的sepread.table()。您也可以在那里设置列名。

      read.table(text = gsub("\\|", "\n", TEST), sep = ":", 
          col.names = c("Variable", "Value"))
      
      #                              Variable    Value
      # 1                             Value01   100.00
      # 2                             Value02   200.00
      # 3                             Value03   300.00
      # 4                             Value04     1.00
      # 5                             Value05     2.00
      # 6 StillAValueButNamesAreNotConsistent 12345.68
      # 7     AlsoNotAllLinesAreTheSameLength     1.00
      

      【讨论】:

        【解决方案3】:

        使用基础 R:

        (我已经分解了每个步骤以希望使代码清晰)

        # your data
        myvec <- c("Value01:100|Value02:200|Value03:300|","Value04:1|Value05:2|",
                   "StillAValueButNamesAreNotConsistent:12345.6789|",
                   "AlsoNotAllLinesAreTheSameLength:1|")
        
        # convert into one long string
        all_text_str <- paste0(myvec, collapse="")
        
        # split the string by "|"
        all_text_vec <- unlist(strsplit(all_text_str, split="\\|"))
        
        # split each "|"-group by ":"
        data_as_list <- strsplit(all_text_vec, split=":")
        
        # collect into a dataframe
        df <- do.call(rbind, data_as_list)
        
        # clean up the dataframe by adding names and converting value to numeric
        names(df) <- c("variable", "value")
        df$value <- as.numeric(df$value)
        

        【讨论】:

          【解决方案4】:

          借助strsplitunlist 函数。每个命令都显示在下面的输出中。

          输入

           TEST
           # [1] "Value01:100|Value02:200|Value03:300|"           
           # [2] "Value04:1|Value05:2|"                           
           # [3] "StillAValueButNamesAreNotConsistent:12345.6789|"
           # [4] "AlsoNotAllLinesAreTheSameLength:1|"             
          

          | 分割,然后由: 分割

           my_list <- strsplit(unlist(strsplit(TEST, "|", fixed = TRUE)), ":", fixed = TRUE)
           my_list
           # [[1]]
           # [1] "Value01" "100"    
          
           # [[2]]
           # [1] "Value02" "200"    
          
           # [[3]]
           # [1] "Value03" "300"    
          
           # [[4]]
           # [1] "Value04" "1"      
          
           # [[5]]
           # [1] "Value05" "2"      
          
           # [[6]]
           # [1] "StillAValueButNamesAreNotConsistent" "12345.6789"                         
          
           # [[7]]
           # [1] "AlsoNotAllLinesAreTheSameLength" "1"                              
          

          将以上列表转换为 data.frame

           df <- data.frame(matrix(unlist(my_list), ncol = 2, byrow=TRUE))
           df
           #                                    X1         X2
           # 1                             Value01        100
           # 2                             Value02        200
           # 3                             Value03        300
           # 4                             Value04          1
           # 5                             Value05          2
           # 6 StillAValueButNamesAreNotConsistent 12345.6789
           # 7     AlsoNotAllLinesAreTheSameLength          1
          

          数据框的列名

           names(df) <- c("Variable", "Value")
           df
           #                              Variable      Value
           # 1                             Value01        100
           # 2                             Value02        200
           # 3                             Value03        300
           # 4                             Value04          1
           # 5                             Value05          2
           # 6 StillAValueButNamesAreNotConsistent 12345.6789
           # 7     AlsoNotAllLinesAreTheSameLength          1
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-09-28
            • 2017-03-29
            • 1970-01-01
            • 2021-06-18
            • 2021-12-29
            • 1970-01-01
            • 2021-11-23
            相关资源
            最近更新 更多