Search code examples
rdataframecsvexport-to-csv

Limiting characters in cells to a specific number while exporting data to a .csv file


This is regarding an issue that I'm facing while exporting data to a .csv file. I'm working with a large dataframe in R. It contains few cells with characters greater than 32767, which is apparently the maximum that a cell can accomodate. When I export the data to a .csv file the content of these cells spills over into the next row. The dataframe however looks completely fine once the .csv file is imported into RStudio. Is there a way to limit the number of characters in each cell to 32767 while exporting the data?


Solution

  • I found the solution to my question in the answers posted here.

    Let's say df is the dataframe which I want to export to a .csv file. The following code does the job

    df <- as.data.frame(substr(as.matrix(df), 1, 32767))
    write.csv(df, <file>, rownames = FALSE)