Question

I want to assign column names for a data table that I read from a file, but I get an error message that doesn't quite make sense to me. This is an excerpt from the data file:

HEADER  SPOT    GRID    TOP LEFT    BOT RIGHT   ROW COL CH1I    CH1B    CH1AB   CH2I    CH2B    CH2AB   SPIX    BGPIX   EDGE    RAT2    MRAT    REGR    CORR    LFRAT   CH1GTB1 CH2GTB1 CH1GTB2 CH2GTB2 CH1EDGEA    CH2EDGEA    FLAG    CH1KSD  CH1KSP  CH2KSD  CH2KSP
REMARK  SOFTWARE    ScanAlyze
REMARK  SOFTVERS    2.30
REMARK  CH1 IMAGE   lc8n076_532 nm
REMARK  CH2 IMAGE   lc8n076_635 nm
REMARK  GRID FILE   ..\..\AshGrids\lc8n076_ar_finalflagged.SAG
REMARK  DATE    8/28/99
REMARK  TIME    9:12:58 AM
SPOT    1   1   79  48  96  65  1   1   187 174 183 115 101 104 225 990 0   1.077   0.6214  0.2045  0.3047  0.3411  0.5111  0.4978  0.1911  0.2089  0.006432    -0.00362    0   0.04242 8.898E-01   0.09556 6.597E-02
0...

Now, I read the header in first:

blood_title <- read.table("lc8n076rex2.DAT", header=FALSE, sep="\t", nrows=1)

And next, I use it as col.names to read the rest of the table, where I want to use the blood_title variable as column names:

blood_data <- read.table("lc8n076rex2.DAT", header=FALSE, skip=8, nrows=5, col.names="blood_name")

Unfortunately, I get an error message that I don't understand:

Error in read.table("lc8n076rex2.DAT", header = FALSE, skip = 8, nrows = 5,  : 
  more columns than column names

Why is this error showing up? Both, the table and the column names in blood_title have consists of 34 columns:

 V1   V2   V3  V4   V5  V6    V7  V8  V9  V10  V11   V12  V13  V14   V15  V16   V17  V18  V19  V20  V21  V22   V23     V24     V25
1 HEADER SPOT GRID TOP LEFT BOT RIGHT ROW COL CH1I CH1B CH1AB CH2I CH2B CH2AB SPIX BGPIX EDGE RAT2 MRAT REGR CORR LFRAT CH1GTB1 CH2GTB1
      V26     V27      V28      V29  V30    V31    V32    V33    V34
1 CH1GTB2 CH2GTB2 CH1EDGEA CH2EDGEA FLAG CH1KSD CH1KSP CH2KSD CH2KSP




    V1 V2 V3 V4  V5 V6  V7 V8 V9  V10 V11 V12   V13 V14 V15 V16 V17 V18     V19     V20    V21    V22     V23    V24    V25    V26
1 SPOT  1  1 79  48 96  65  1  1  187 174 183   115 101 104 225 990   0  1.0770  0.6214 0.2045 0.3047 0.34110 0.5111 0.4978 0.1911
2 SPOT  2  1 78  65 95  82  1  2  451 166 177   842 101 133 225 756   0  2.6000  2.1550 1.9440 0.7322 3.40300 0.7467 0.7467 0.5067
3 SPOT  3  1 78  83 95 100  1  3  727 171 187 12803  98 134 225 766   0 22.8500 20.1900 0.0000 0.0000 0.06008 0.7289 0.8711 0.5600
4 SPOT  4  1 78 100 95 117  1  4 1532 181 196   730  98 108 225 746   0  0.4678  0.4773 0.4028 0.8788 0.42010 0.7867 0.8000 0.6311
5 SPOT  5  1 78 118 95 135  1  5 4139 188 214 20358 105 159 225 746   0  5.1260  4.7240 0.0000 0.0000 5.58600 0.8533 0.9289 0.7156
     V27        V28        V29 V30     V31       V32     V33       V34
1 0.2089  0.0064320 -3.620e-03   0 0.04242 8.898e-01 0.09556 6.597e-02
2 0.5689 -0.0007581 -2.205e-03   0 0.38510 3.391e-23 0.46000 6.426e-33
3 0.7600 -0.0164600  8.613e-06   0 0.44400 8.761e-31 0.65230 0.000e+00
4 0.6356 -0.0104900  6.777e-03   0 0.51220 1.389e-40 0.52090 5.747e-42
5 0.8356 -0.0209900 -9.983e-03   0 0.60900 0.000e+00 0.70760 0.000e+00

EDIT:

This is the complete code that I used in this particular order

> blood_title <- read.table("lc8n076rex2.DAT", header=FALSE, sep="\t", nrows=1)
> blood_data <- read.table("lc8n076rex2.DAT", header=FALSE, skip=8, nrows=5, col.names=unlist("blood_title"))
Error in read.table("lc8n076rex2.DAT", header = FALSE, skip = 8, nrows = 5,  : 
  more columns than column names
> blood_title
      V1   V2   V3  V4   V5  V6    V7  V8  V9  V10  V11   V12  V13  V14   V15  V16   V17  V18  V19  V20  V21  V22   V23     V24     V25
1 HEADER SPOT GRID TOP LEFT BOT RIGHT ROW COL CH1I CH1B CH1AB CH2I CH2B CH2AB SPIX BGPIX EDGE RAT2 MRAT REGR CORR LFRAT CH1GTB1 CH2GTB1
      V26     V27      V28      V29  V30    V31    V32    V33    V34
1 CH1GTB2 CH2GTB2 CH1EDGEA CH2EDGEA FLAG CH1KSD CH1KSP CH2KSD CH2KSP
> blood_data
    V1 V2 V3 V4  V5 V6  V7 V8 V9  V10 V11 V12   V13 V14 V15 V16 V17 V18     V19     V20    V21    V22     V23    V24    V25    V26
1 SPOT  1  1 79  48 96  65  1  1  187 174 183   115 101 104 225 990   0  1.0770  0.6214 0.2045 0.3047 0.34110 0.5111 0.4978 0.1911
2 SPOT  2  1 78  65 95  82  1  2  451 166 177   842 101 133 225 756   0  2.6000  2.1550 1.9440 0.7322 3.40300 0.7467 0.7467 0.5067
3 SPOT  3  1 78  83 95 100  1  3  727 171 187 12803  98 134 225 766   0 22.8500 20.1900 0.0000 0.0000 0.06008 0.7289 0.8711 0.5600
4 SPOT  4  1 78 100 95 117  1  4 1532 181 196   730  98 108 225 746   0  0.4678  0.4773 0.4028 0.8788 0.42010 0.7867 0.8000 0.6311
5 SPOT  5  1 78 118 95 135  1  5 4139 188 214 20358 105 159 225 746   0  5.1260  4.7240 0.0000 0.0000 5.58600 0.8533 0.9289 0.7156
     V27        V28        V29 V30     V31       V32     V33       V34
1 0.2089  0.0064320 -3.620e-03   0 0.04242 8.898e-01 0.09556 6.597e-02
2 0.5689 -0.0007581 -2.205e-03   0 0.38510 3.391e-23 0.46000 6.426e-33
3 0.7600 -0.0164600  8.613e-06   0 0.44400 8.761e-31 0.65230 0.000e+00
4 0.6356 -0.0104900  6.777e-03   0 0.51220 1.389e-40 0.52090 5.747e-42
5 0.8356 -0.0209900 -9.983e-03   0 0.60900 0.000e+00 0.70760 0.000e+00
> 

EDIT2:

Problem solved, forgot to unquote the variable blood_title

Was it helpful?

Solution

There're two things here. First you're saving the names to blood_title and then using col.names=blood_name in blood_data. I suppose this is a typo...?

Second (and more importantly), your actual issue is that you'r providing a just a character vector with one value "blood_name" to col.names instead of all the column names. Try this:

blood_data <- read.table("lc8n076rex2.DAT", header=FALSE, 
                    skip=8, nrows=5, col.names=unlist(blood_title))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top