Help with pwalk, split dataframe into different CSVs

I have a dataframe call "Data" below, I want to split it by the first column and create separate CSV files with it.

For some reason when I got to the pwalk function, to do the export CSV by each file, it gives me an error.

The error says it's not a dataframe, but even when I convert it to a dataframe still throws an error.

any help would be great!

Data = data.frame(
  stringsAsFactors = FALSE,
                 A = c(1L, 2L, 3L, 4L),
                 B = c("a", "b", "c", "d")

Files = split(Data, Data$A)

  pwalk(Files, ~write_csv(Files, path = paste0(Files, ".csv"))) ```

You can't use Files inside write.csv. And you should use iwalk not pwalk in this case.
Try this code.

Files |> 
iwalk(~write_csv(.x, file = paste0(.y, ".csv")))

iwalk basically converts the names of the elements as .y.

Hope this helps.


1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.