Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

Convert columns a data frame to a list in R

I want to convert the columns in data frame to a list. The format of data frame is described as follows:

   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

H1, E9, and F12 are the file names, and I need to convert them into a list, i.e., each file will be one element of the list, and for each element, it is a data frame, with time and response as the column names.

Thank you for your help.

question from:https://stackoverflow.com/questions/65849324/convert-columns-a-data-frame-to-a-list-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A base R option using split.default + gsub

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

gives

$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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...