0

I have named list of dataframes, i.e.:

dfs <- list("foo" = df1,
"bar" = df2,
"baz" = df3,
...)

I want to write each df in separate csv file with name as in the list, for that I have a simple loop:

    for (i in c(1: length(dfs))) {

      write.table(dfs[i], 
              names(dfs)[i],
              row.names = FALSE,
              sep = ",",
              fileEncoding = "Windows-1252")
}

It works nice, except the output csv files get prefix in the column names, which equals to the df name in the list, i.e. the columns of first df in csv (file "foo.csv") become "foo.Column1", "foo. Column2", etc. I do not have those prefixes in my original dataframes and want to them not to get written in csv file. Is it possible? Thanks!

Marqiz
  • 1
  • 2
  • 1
    To extract a value from a list use double `[]` otherwise you'll get a single element list containing the extracted element. The prefix is coming from is enclosing list. So `dfs[[i]]` will be the answer – Billy34 Aug 29 '22 at 14:15
  • 1
    Just a comment to make sure this is very clear: change `write.table(dfs[i],` to `write.table(dfs[[i]],` because `dfs[i]` is a single-element list containing a data frame whereas `dfs[[i]]` is the data frame. – Gregor Thomas Aug 29 '22 at 14:35

0 Answers0