Unimplemented type list when trying to write.table

前端 未结 6 2131
说谎
说谎 2020-12-08 06:45

I have the following data.table (data.frame) called output:

> head(output)
        Id                                           Title IsProhibited
1 10000         


        
6条回答
  •  时光取名叫无心
    2020-12-08 07:26

    Those are all elegant solutions.

    For the curious reader who would prefer some R-code to ready made packages , here's an R-function that returns a non-list dataframe that can be exported and saved as .csv.

    output is the "troublesome" data frame in question.

    df_unlist<-function(df){
    
    df<-as.data.frame(df)
    
    nr<-nrow(df)
    
    c.names<-colnames(df)
    
    lscols<-as.vector(which(apply(df,2,is.list)==TRUE))
    
    if(length(lscols)!=0){
    
    for(i in lscols){
    
    temp<-as.vector(unlist(df[,i]))
    
    if(length(temp)!=nr){
    
    adj<-nr-length(temp)
    
    temp<-c(rep(0,adj),temp)
    
    }
    
    df[,i]<-temp
    
    } #end for
    
    df<-as.data.frame(df)
    
    colnames(df)<-c.names
    }
    return(df)
    }
    

    Apply the function on dataframe "output" :

    newDF<-df_unlist(output)
    

    You can next confirm that the new (newDF) data frame is not 'listed' via apply(). This should successfully return FALSE.

    apply(newDF,2,is.list)         #2 for column-wise step.
    

    Proceed to save the new dataframe, newDF as a .csv file to a path of your choice.

    write.csv(newDF,"E:/Data/newDF.csv")
    

提交回复
热议问题