1

I would like to write a multiple dataframe "neighbours_dataframe" in a single CSV file :

I use this line to write the multiple dataframe to multiple file :

for(i in 1:vcount(karate)){
write.csv(neighbours_dataframe[[i]], file = as.character(V(karate3)$name[i]),row.names=FALSE)}

if I use this code:

for(i in 1:vcount(karate)){
write.csv(neighbours_dataframe[[i]], file = "karate3.csv",row.names=FALSE)}

this would give me just the last dataframe in the csv file :

I was wondering , How could I have a single CSV file which have all the dataframe in the way that the column header of the first dataframe just written to the csv file and all other data frame copied in a consecutive manner ?

thank you in advance

5
  • 1
    If all data.frames have the same set of column names, try do.call(rbind, neighbours_dataframe) (assuming neighbours_dataframe is a list of data.frames), then write the result of that to a csv. If they don't all share the same columns, then try dplyr::rbind_all(neighbours_dataframe). Commented Nov 17, 2014 at 7:47
  • 2
    Set append=TRUE and it should work. Commented Nov 17, 2014 at 7:49
  • 1
    @Neal append won't work for write.csv - see my comment below. Commented Nov 17, 2014 at 8:50
  • thak you @Neal as jbaums said append didn't work and I get the warnings and also it was just like the second line code nothing happened Commented Nov 17, 2014 at 11:15
  • thank you so much @jbaums, it just work well Commented Nov 17, 2014 at 11:16

1 Answer 1

2

Two methods; the first is likely to be a little faster if neighbours_dataframe is a long list (though I haven't tested this).

Method 1: Convert the list of data frames to a single data frame first

As suggested by jbaums.

library(dplyr)
neighbours_dataframe_all <- rbind_all(neighbours_dataframe)
write.csv(neighbours_dataframe_all, "karate3.csv", row.names = FALSE)

Method 2: use a loop, appending

As suggested by Neal Fultz.

for(i in seq_along(neighbours_dataframe))
{
  write.table(
    neighbours_dataframe[[i]], 
    "karate3.csv", 
    append    = i > 1, 
    sep       = ",", 
    row.names = FALSE,
    col.names = i == 1
  )
}
Sign up to request clarification or add additional context in comments.

4 Comments

append isn't possible for write.csv. See ?write.table, which states, for CSV files, "These wrappers are deliberately inflexible: they are designed to ensure that the correct conventions are used to write a valid file. Attempts to change append, col.names, sep, dec or qmethod are ignored, with a warning.". Instead, you might have to do something like: write.table(neighbours_dataframe[[i]], "karate3.csv", append = i > 1, row.names = FALSE, col.names = i==1, sep=',') (within your for loop).
@jbaums Good spot; fixed.
I think you do need to suppress row names as well, though, otherwise column names aren't correctly aligned. See L <- data.frame(a=1:3, b=1:3); L <- list(d, d); f <- tempfile(fileext='.csv'); for(i in seq_along(L)) write.table(L[[i]], f, append=i>1, sep=",", col.names=i==1); file.show(f).
thank you @RichieCotton, the first method just work perfectly fine on rather long list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.