且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将列表转换为数据框,同时将其标题保留在R中

更新时间:2022-12-12 08:48:07

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

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)