-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
9
  • 1
    What have you tried so far, and what errors did you encounter? Commented May 23, 2018 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! Commented May 23, 2018 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. Commented May 23, 2018 at 22:22
  • Sorry that was my bad, I mean first row contains row names! And position 1, then 25 and so on... Commented May 23, 2018 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. Commented May 24, 2018 at 0:05

3 Answers 3

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="")
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

-1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.