write.csv() a list of unequally sized data.frames

前端 未结 2 393
太阳男子
太阳男子 2020-12-06 11:58

I would like to pump out a list of data.frame() objects to a csv file so I can work on it for presentation. I\'m finding that it\'s replying with an error:

         


        
相关标签:
2条回答
  • 2020-12-06 12:29

    That's a warning, not an error. You can't change append=FALSE with write.csv. ?write.csv says:

    Attempts to change ‘append’, ‘col.names’, ‘sep’, ‘dec’ or ‘qmethod’ are ignored, with a warning.

    Use write.table with sep="," instead.

    0 讨论(0)
  • 2020-12-06 12:38

    You can now export multiple dataframes in a single CSV using sheetr

    install.packages("devtools")
    library(devtools)
    
    # Install package from github
    install_github('d-notebook/sheetr')
    library(sheetr)
    
    # Create a list of dataframes
    iris_dataframe = list()
    iris_dataframe[["Setosa subset"]] = head(iris[iris$Species == "setosa",])
    iris_dataframe[["Versicolor subset"]] = head(iris[iris$Species == "versicolor",])
    
    # Write the list of dataframes to CSV file
    write_dataframes_to_csv(iris_dataframe, "exmaple_csv.csv")
    

    Which will export:

    screenshot

    .
    .

    Or, if you prefer to do that manually, you can use sink files:

    # Sample dataframes:
    df1 = iris[1:5, ]
    df2 = iris[20:30, ]
    
    # Start a sink file with a CSV extension
    sink('multiple_df_export.csv')
    
     # Write the first dataframe, with a title and final line separator 
    cat('This is the first dataframe')
    write.csv(df1)
    cat('____________________________')
    
    cat('\n')
    cat('\n')
    
    # Write the 2nd dataframe to the same sink
    cat('This is the second dataframe')
    write.csv(df2)
    cat('____________________________')
    
    # Close the sink
    sink()
    
    0 讨论(0)
提交回复
热议问题