如何将列表转换为数据框,同时将其标题保留在R中 [英] How to convert a list into a data frame while keeping its headers in R

查看:121
本文介绍了如何将列表转换为数据框,同时将其标题保留在R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 mylist 的列表,其中除标题外还有108行.我尝试将其转换为数据框,并使用以下命令成功运行了

I have a a list called mylist, in it there are 108 rows apart from the headers. I tried converting it to a dataframe and it successfully worked using the following command

df <- data.frame(matrix(unlist(mylist), nrow=108, byrow=F))

但是,我的列表(mylist)中每个矩阵的标题尚未在我的新数据帧df中定义.

However, the headers for each matrix in my list (mylist) have not been defined in my new dataframe df.

names(df)

"X1" "X2" "X3" "X4" "X5" "X6" "X7" "X8" "X9" "X10" "X11" "X12" "X13" "X14" "X15" "X16" "X17" "X18" "X19" "X20" "X21" "X22" "X23" "X24" "X25" "X26" "X27" "X28" "X29" "X30" "X31" "X32" "X33" "X34" "X35" "X36"

mylist 看起来像这样

head(mylist[[1]])

      RAmi         MDaf
1   11.806405   -3.588567
2   7.711101    -9.721415
3   2.315104    11.217575
4   20.372999   -2.267938
5   22.279704   -1.668082
6   13.57909    20.67355



head(mylist[[2]])

          Tomi         Rahaf
    1     325          -3
    2      71          -9
    3      2           11
    4     20.999      -22
    5      22         -16
    6     139         2065

这仅适用于 i = 1 时的 head(mylist [[i]]),但是对于i = 1,2,3,..存在类似的情况..,18

this is only for head(mylist[[i]]) when i=1 but there exist similar things for i=1,2,3, ... , 18

我想要的是将它们全部相邻放置在一个数据帧中.一切正常,但标题出现问题

What I want is to put them all in one dataframe adjacently. It worked fine but I am having a problem with the heading

我希望有人可以帮助我了解如何去做.谢谢

I hope one may help me with understanding how to do so. Thank you

推荐答案

似乎将一个数据帧列表转换为一个数据帧存在问题.因此,最好从一开始就使用一个数据框.

It seems that there is a problem in converting a list of dataframes into one dataframe. Therefore, it would be better to work with one dataframe from the very beginning.

例如,在我的情况下,我导入了原始数据

For example, in my case i imported the raw data

#data1=read.csv(file.choose(), header=F)
data1=read.csv

然后,我开始分析我的数据.然后,我像

Then afterwards I started analysing my data. I then wrote a code to generate a dataframe as in

# Here we are extracting the data which we wish to have
#iterations=nrow(data1) #Check the number of rows

listOfDataFrames <- vector(mode = "list", length = 18)   # define a dataframe with length
#mylist <- list() #create an empty list
for (k in 0:17)
{
    j=18 # from 14 i.e. switzerland this is not true anymore 
    if ((k>=14) && (k<16))
    {
        f=7+j*k
        s=3+j*k

    }  else if (k<14)
    {
        s=7+j*k-4
        f=7+j*k
    } else if (k==16)
    {
        f=j*k+5
        s=f+4
    } else if (k==17)
    {
        f=304
        s=301
    }
    b= data1[,c(f,s)] 
    #mylist[[k+1]] <- b
    listOfDataFrames[[k+1]]=b
}
#df <- data.frame(matrix(unlist(mylist), nrow=108, byrow=F))

这使我可以将所有数据和标题一起放入一个数据帧中

This allowed me to put all the data into one dataframe along with the headers

df=do.call("cbind", listOfDataFrames)
head(df)

这篇关于如何将列表转换为数据框,同时将其标题保留在R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆