简体   繁体   中英

read.table row.names & col.names ERROR

This is very annoying, I can't seam to understand whats wrong?

This is my .csv file:

            D102-A1 D102-A2
A1BG        0.32    0.39
A1BG-AS1    0.08    0.14

The actual look:
;D102-A1;D102-A2
A1BG;0.32;0.39
A1BG-AS1;0.08;0.14

This is my code:

x <- read.table(file = "file.csv", sep = ";", header = TRUE, col.names = 1)

This gives these errors:

more columns than column names In addition: Warning message: header and 'col.names' are of different lengths

The first line as an empty cell in upper left corner, should this not be a header? When I have a header, why can't I set column 1 as row.names?

Thank you!

I am not sure as I have never used R but I was looking at a manual page http://cran.r-project.org/doc/manuals/R-intro.html#The-read_002etable_0028_0029-function and it suggests that you need column headings for all data columns if there are not row labels.

have you tried:

read.table(file = "file.csv", sep = ";")

and then looked at the results?

This makes me think you need to test without the colnames option

To read an entire data frame directly, the external file will normally have a special form.

The first line of the file should have a name for each variable in the data frame.

Each additional line of the file has as its first item a row label and the values for each variable.

The point I am trying to make is that the note suggests that without a column name over the first column the program is having difficulty interpreting the data. How would the data be named if there is no column heading?

From ?read.table , the optional argument col.names is a vector of names for the column. So by specifying col.names = 1 you are saying that there is a single column, which has name 1 .

To fix this, you can drop the col.names argument

test1 <- ";D102-A1;D102-A2
A1BG;0.32;0.39
A1BG-AS1;0.08;0.14"
tf <- tempfile()
writeLines(test1, tf)

x <- read.table(tf, sep=";", header=TRUE)

This however leaves you with an X as the column name in the first "cell". If this is acceptable, then you can stick with this; but if it is not, your data may need to be melted or modified again, but to advise you on this we'd need to know what data frame you're hoping to produce.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM