【问题标题】:Convert columns a data frame to a list in R将数据框的列转换为R中的列表
【发布时间】:2021-01-22 16:47:22
【问题描述】:

我想将数据框中的列转换为列表。数据框格式说明如下:

   H1.time H1.response E9.time E9.response F12.time F12.response
1:     0.0  0.00000000     0.0  0.00000000      0.0   0.00000000
2:     0.2  0.00142469     0.2  0.00826733      0.2   0.00703381
3:     0.4 -0.00418229     0.4  0.01416873      0.4   0.00863728
4:     0.6  0.00361758     0.6  0.00845066      0.6   0.00739067
5:     0.8  0.00281592     0.8  0.01258872      0.8   0.00786157
6:     1.0 -0.00293035     1.0  0.01097368      1.0   0.00679848

H1E9F12是文件名,我需要将它们转换成一个列表,即每个文件都会是列表的一个元素,对于每个元素,它都是一个数据框,以 timeresponse 作为列名。

感谢您的帮助。

【问题讨论】:

    标签: r list dataframe data-manipulation


    【解决方案1】:

    使用split.default + gsub 的基本 R 选项

    split.default(df, gsub("\\..*", "", names(df)))
    

    给予

    $E9
       E9.time E9.response
    1:     0.0  0.00000000
    2:     0.2  0.00826733
    3:     0.4  0.01416873
    4:     0.6  0.00845066
    5:     0.8  0.01258872
    6:     1.0  0.01097368
    
    $F12
       F12.time F12.response
    1:      0.0   0.00000000
    2:      0.2   0.00703381
    3:      0.4   0.00863728
    4:      0.6   0.00739067
    5:      0.8   0.00786157
    6:      1.0   0.00679848
    
    $H1
       H1.time H1.response
    1:     0.0  0.00000000
    2:     0.2  0.00142469
    3:     0.4 -0.00418229
    4:     0.6  0.00361758
    5:     0.8  0.00281592
    6:     1.0 -0.00293035
    

    【讨论】:

      【解决方案2】:

      尝试重塑:

      library(dplyr)
      library(tidyr)
      #Code
      List <- df %>% 
        dplyr::mutate(id=row_number()) %>%
        pivot_longer(-id) %>%
        separate(name,c('V1','V2'),sep='\\.') %>%
        pivot_wider(names_from = V2,values_from=value) %>%
        select(-id) %>%
        group_split(V1)
      

      输出:

      [[1]]
      # A tibble: 6 x 3
        V1     time response
        <chr> <dbl>    <dbl>
      1 E9      0    0      
      2 E9      0.2  0.00827
      3 E9      0.4  0.0142 
      4 E9      0.6  0.00845
      5 E9      0.8  0.0126 
      6 E9      1    0.0110 
      
      [[2]]
      # A tibble: 6 x 3
        V1     time response
        <chr> <dbl>    <dbl>
      1 F12     0    0      
      2 F12     0.2  0.00703
      3 F12     0.4  0.00864
      4 F12     0.6  0.00739
      5 F12     0.8  0.00786
      6 F12     1    0.00680
      
      [[3]]
      # A tibble: 6 x 3
        V1     time response
        <chr> <dbl>    <dbl>
      1 H1      0    0      
      2 H1      0.2  0.00142
      3 H1      0.4 -0.00418
      4 H1      0.6  0.00362
      5 H1      0.8  0.00282
      6 H1      1   -0.00293
      

      使用的一些数据:

      #Data
      df <- structure(list(H1.time = c(0, 0.2, 0.4, 0.6, 0.8, 1), H1.response = c(0, 
      0.00142469, -0.00418229, 0.00361758, 0.00281592, -0.00293035), 
          E9.time = c(0, 0.2, 0.4, 0.6, 0.8, 1), E9.response = c(0, 
          0.00826733, 0.01416873, 0.00845066, 0.01258872, 0.01097368
          ), F12.time = c(0, 0.2, 0.4, 0.6, 0.8, 1), F12.response = c(0, 
          0.00703381, 0.00863728, 0.00739067, 0.00786157, 0.00679848
          )), class = "data.frame", row.names = c("1:", "2:", "3:", 
      "4:", "5:", "6:"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-14
        • 1970-01-01
        • 2023-02-23
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        相关资源
        最近更新 更多