Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a data table into R as a matrix

Tags:

r

This code is directly from a Bioconductor vignette for creating an expressionSet (http://www.bioconductor.org/packages/2.12/bioc/vignettes/Biobase/inst/doc/ExpressionSetIntroduction.pdf).

>  exprs <- as.matrix(read.table(exprsFile, header=TRUE, sep = "\t",
                 + row.names = 1,
                 + as.is=TRUE))

Can anyone tell why this code generates the following error message?

> exprs <- as.matrix(read.table(exprsFile, header=TRUE, sep = "\t",
+                       row.names = 1,
Error: unexpected '=' in:
"exprs <- as.matrix(read.table(exprsFile, header=TRUE, sep = "\t",
                 + row.names ="
>                      + as.is=TRUE))
Error: unexpected ')' in "                     + as.is=TRUE)"

Or, could you suggest an alternative method to read in the file exprsFile as a matrix with header?

Thank you very much for your time.

like image 812
user2939281 Avatar asked Oct 30 '13 22:10

user2939281


People also ask

How do I convert a data table to a matrix in R?

To convert a table into matrix in R, we can use apply function with as. matrix. noquote function.

How do you create a matrix from a table?

To create a matrix, you start with a table and convert it to a matrix. On the Design tab > Switch Visualizations > Table > Matrix.

How do you store data in a matrix in R?

When creating matrices, we can pass the matrix function a vector containing the numbers to put in the matrix. If we want to specify the number of columns or rows, we can do that using the ncol and nrow arguments. By default, R takes the input vector and fills each column.


1 Answers

The biobase vignette is (almost certainly) produced using Sweave. The > and leading + where single expressions have been split over multiple lines are an artefact of using Sweave and how it displays code it processes. It reflects how the terminal / console (R session) would looked if you had entered the following (which should work)

exprs <- as.matrix(read.table(exprsFile, header=TRUE, sep = "\t",
                  row.names = 1,
                  as.is=TRUE))

knitr (which is an alternative to Sweave, and is now permitted for vignettes, by default has these prompts removed, so code is more directly copy-and-pastable.

like image 129
mnel Avatar answered Oct 10 '22 12:10

mnel