-1

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
smci
  • 32,567
  • 20
  • 113
  • 146
SaraMA
  • 25
  • 1
  • 8
  • 1
    What have you tried so far, and what errors did you encounter? – Kerry Jackson May 23 '18 at 21:12
  • I have tried:write.table(dfGL, file = "C:/Users/sara/Desktop/Inputttt.txt", row.names = FALSE, sep = "\t\t", quote = F) But it does not give me what I want! – SaraMA May 23 '18 at 21:19
  • 1
    Can make no sense of this. "First row is rownames"? What does that mean? There is no "position 0" in R. Numbering starts with 1. – IRTFM May 23 '18 at 22:22
  • Sorry that was my bad, I mean first row contains row names! And position 1, then 25 and so on... – SaraMA May 23 '18 at 22:39
  • 1
    What are the data types? Are you looking for a particular separator? Does that separator show up in any of the values of any variable? What operating system are you on? Do you mean that the first element of the first column should take up the first 24 positions in the first row with spaces filled in at the end? I am not really sure what you are asking for. – randy May 24 '18 at 00:05
  • *"But it does not give me what I want!"* tells us nothing at all. Be specific. Downvoted for lack of clarity and reproducibility. Please don't post images of data, **use `dput(head(df))` to post your actual data**. – smci May 25 '18 at 09:49
  • (You're aware that choosing tabs instead of spaces as separator, with 22 columns, your line width will be > 160 characters, at which point most UNIX tools like grep, awk, perl, diff are known to be unreliable, and thus SCM like git? Also impossible to read on console or in diff tool) – smci May 25 '18 at 10:01
  • You don't mean "first row contains row names", you mean "column names", right? (so pressure, diameter, roughness... are column names not row names?). That's also called a header row. And you want the fixed-width format you specify. And you want tabs as separator. And apparently no rownames (that's numbering on each row, as distinct to column names). – smci May 25 '18 at 10:03
  • Yes, I meant col names! – SaraMA May 27 '18 at 00:45

3 Answers3

1

I would suggest using the write.fwf from the gdata package if you want to create a Fixed Width File. The col names don't seem to be saved to the correct position, so to work around this you can format them to the width you want the columns to be using formatC.

library(gdata)
colnames(dfGL) <- formatC(colnames(dfGL), width = 25, flag = " ")
write.fwf(dfGL, file = "C:/Users/sara/Desktop/Inputttt.txt", width = rep(25,ncol(dfGL)), sep="")
Kerry Jackson
  • 1,821
  • 12
  • 20
0

I think that what you are trying to do is to end up with a delimited text file with 24 blank columns between each adjacent variable. Here is a sort of roundabout and clunky solution. It will break if you have any strings with commas in any of your variables, has the annoying quality of having 24 commas written inside a string (you can count them with nchar()), and commits the offense of saving data to disk and then reading them back in.

# Export your data.frame to a csv file
write.csv(YourDataFrame, file="Path/To/File.csv", row.names=FALSE)

# Read in the lines of the file
fileLinesTemp = readLines("Path/To/File.csv")
# Add a bunch of commas to add columns
fileLinesTemp = gsub(",", ",,,,,,,,,,,,,,,,,,,,,,,,", fileLinesTemp)

# Write the new lines back to the file
fileConn = file("Path/To/File.csv")
writeLines(fileLinesTemp, fileConn)
close(fileConn)
randy
  • 1,031
  • 10
  • 20
0

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)
zx8754
  • 52,746
  • 12
  • 114
  • 209
SaraMA
  • 25
  • 1
  • 8