Komal Rathi
Komal Rathi

Reputation: 4274

R: write complex data frame as is to a file

I have the following data frame:

>results[,1:4]

           N=9                                                                      N=8                                            N=7                                                      N=6
HPS4\n(244,492) APOL3\n(235,486)\nAPOL5\n(200,398)\nCELSR1\n(213,432)\nCLTCL1\n(204,402) CPT1B,CHKB-CPT1B\n(175,344)\nPLXNB2\n(205,412) FAM118A\n(121,240)\nMYO18B\n(169,345)\nSEC14L4\n(99,221)

N=9, N=8, N=7...until N=1 is the first row. The second row has comma- & newline-separated values. The dimensions of my data frame are:

dim(results)
[1] 2 9

class(results)
[1] "data.frame"

The problem is that I have tried write.table() using different separator values as well as WriteXLS (of the WriteXLS package) but every single time the output file is messed up. The first row outputs nicely, but the second row gets appended all in the first 2-3 columns. Basically the table does not output as is. I would like to know alternatives where you can write the dataframe without losing the separators between the columns.

dput(results)
structure(list(X9 = c("N=9", "HPS4\n(244,492)"), X8 = c("N=8", 
"APOL3\n(235,486)\nAPOL5\n(200,398)\nCELSR1\n(213,432)\nCLTCL1\n(204,402)"
), X7 = c("N=7", "CPT1B,CHKB-CPT1B\n(175,344)\nPLXNB2\n(205,412)"
), X6 = c("N=6", "FAM118A\n(121,240)\nMYO18B\n(169,345)\nSEC14L4\n(99,221)"
), X5 = c("N=5", "MOV10L1\n(97,230)"), X4 = c("N=4", "CCDC157\n(95,212)\nCECR2\n(121,272)\nSEC14L3\n(93,178)\nTTLL12\n(128,241)\nTXNRD2\n(144,302)\nYDJC,CCDC116\n(108,224)"
), X3 = c("N=3", "CARD10\n(78,165)\nEFCAB6\n(38,69)\nIL17RA\n(59,112)\nPLA2G3\n(69,134)\nRIBC2\n(84,163)\nRTDR1\n(1,2)\nSLC2A11\n(21,53)\nTEX33\n(39,73)\nTTC38\n(14,30)"
), X2 = c("N=2", "APOBEC3H\n(109,233)\nBRD1\n(33,67)\nGNB1L\n(12,31)\nMN1\n(26,52)\nPRAME\n(15,38)\nPRR14L\n(53,109)\nSCARF2\n(80,193)\nSMC1B\n(30,55)\nSUN2\n(73,128)\nTBC1D10A\n(53,129)\nTUBGCP6\n(81,160)\nTYMP,SCO2\n(41,88)"
), X1 = c("N=1", "ALG12\n(58,107)\nAPOBEC3G\n(31,67)\nARVCF\n(7,20)\nBCL2L13\n(6,13)\nBCR\n(9,19)\nCABIN1\n(51,99)\nCDC42EP1\n(5,7)\nCDPF1\n(36,56)\nCRELD2\n(87,159)\nCRYBA4\n(16,26)\nCSNK1E\n(0,1)\nEFCAB6,EFCAB6-AS1\n(1,0)\nEMID1\n(17,26)\nFAM109B\n(0,2)\nFAM227A\n(0,5)\nFAM83F\n(0,4)\nGAB4\n(19,40)\nGGT1,FAM211B\n(0,4)\nGGT5\n(23,37)\nHDAC10\n(35,71)\nISX\n(32,59)\nKCTD17\n(0,0)\nMICAL3\n(6,15)\nPACSIN2\n(5,9)\nPEX26\n(14,20)\nPIWIL3\n(36,60)\nPNPLA3\n(96,171)\nPNPLA5\n(20,19)\nPPP6R2\n(3,5)\nSCUBE1\n(2,3)\nSELO\n(10,18)\nSEZ6L\n(14,30)\nSGSM1\n(14,37)\nTBX1\n(99,184)\nTCN2\n(31,57)\nTMPRSS6\n(4,17)\nTRMU\n(39,84)\nZBED4\n(1,0)\nZNF74\n(1,9)"
)), .Names = c("X9", "X8", "X7", "X6", "X5", "X4", "X3", "X2", 
"X1"), row.names = c(NA, -2L), class = "data.frame")

This is the kind of output I am expecting.

I just want to make a file out of it, in any format, but representing exactly the kind of table that I have shown in the link. I do not want to import it again in R or any other software.

Upvotes: 0

Views: 236

Answers (1)

Spacedman
Spacedman

Reputation: 94212

If I simply:

> write.csv(results,"results.csv")

then read into Open/Libre Office spreadsheet:

enter image description here

which looks like your sample spreadsheet output. Chop the heading and first column out if you want.

Did you try this? Does Excel fail? Install Open/Libre office instead.

Note your dropbox example had no newline between "ALG12 (58,107)" whereas your sample data does, so my J3 cell is a bit crammed and falls off the bottom.

Upvotes: 1

Related Questions