0

In a previous question, I received output for an R dataframe that had two aligned columns. The answer gave me the following output:

Dataframe output - columns separated by variable number of space characters

While the post answered my initial question, it seems as if the program I intend to use requires a text file in which the two columns are both aligned and separated by a single character (e.g. a tab). The previous solution instead results in a large and variable number of spaces between the first and second columns (depending on the length of the string in the first column for that particular row.) Inserting a single character, however, results in a misalignment of the columns.

Is there any way in which I can replace a large number of spaces with a single character that has variable spacing to 'reach' to the second column?

If it helps, this webpage contains a .txt file that you may download to see the intended output (although it does not suffer from the problem with the first column having variable name lengths, it has a single 'space character' that separates the first and second columns. If I 'copy and paste' this specific space character between columns 1 and 2, the program can successfully interpret the .txt file. This copy + paste results in a single character separating the columns and appropriate alignment.)

For further example, the first of the following pictures (note the highlight is a single character) properly parses while the second does not:

enter image description here

enter image description here

xfrostyy
  • 37
  • 5
  • Sounds like a regular expression task `gsub("\\s+", "\t", x)` or something like that to replace any number of consecutive spaces with a tab. – thelatemail Jul 23 '21 at 05:05
  • @thelatemail this unfortuantely did not work. It instead printed two rows of data with the first row reading `c("CHEK2 ", "MYL2 ",...` and the second row reading `c("-2.869351", "-2.477345", "-2.473197",...` – xfrostyy Jul 23 '21 at 16:41

0 Answers0