将数据帧中的列转换为R中的列表

将数据帧中的列转换为R中的列表,r,list,dataframe,data-manipulation,R,List,Dataframe,Data Manipulation,我想将数据框中的列转换为列表。数据帧的格式描述如下: 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

我想将数据框中的列转换为列表。数据帧的格式描述如下:

   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作为列名

感谢您的帮助。

尝试重塑:

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)
库(dplyr)
图书馆(tidyr)
#代码
列表%
dplyr::mutate(id=row_number())%>%
枢轴长度(-id)%>%
单独的(名称,c('V1','V2'),sep='\\.')%>%
枢轴(名称从=V2,值从=value)%>%
选择(-id)%%>%
分组(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
[[1]]
#一个tibble:6x3
V1时间响应
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]]
#一个tibble:6x3
V1时间响应
一楼二〇〇
2 F12 0.2 0.00703
3 F12 0.4 0.00864
4 F12 0.6 0.00739
5 F120.80.00786
6 F12 1 0.00680
[[3]]
#一个tibble:6x3
V1时间响应
1 H10 0
2 H1 0.2 0.00142
3 H1 0.4-0.00418
4 H1 0.6 0.00362
5 H10.80.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:"))
#数据

df使用
split的基本R选项。默认值
+
gsub

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