Search code examples
rfile-iofixed-format

Creating txt file from a dataframe with specific fixed-width format


I have a data frame called dfGL with 22 columns and 17000 rows! The row names are: pressure, diameter, roughness...

I want to create a txt file from this data frame such that:

  • 1st column of dfGL starts from position 1 of the text file (Line 1 Column 1),
  • 2nd column starts at position 25 (Line 1 Column 25),
  • 3rd column starts at position 50 (Line 1 Column 50),
  • and so on! enter image description here

Solution

  • Here is the solution that works:

    library(gdata)
    
    dfGL <- rbind(colnames(dfGL) , dfGL)
    write.fwf(dfGL, file = "Inpuuuttt.txt", 
              width = 25,rownames = TRUE, colnames = FALSE,  quote = FALSE)