1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

R: Saving and Reading Matrix in R

Posted at

R has a very difficult, yet probally extremely powerful scheme called "dataframe". Because of its intrication, saving and reading can be difficult for a beginner like me.

Suppose Mat is Matrix. The following will save in the text file that is readable from Matlab.

write.table(Mat, "Matrix", sep = ",", row.names = FALSE, col.names = FALSE)

Howver, the matrix saved this way cannot be read from R. When R reads a text file, it considers the column as "names" in data.frame attributes. Without the prescribed names, R will confuse names with values.

data.frame attributes:
names: category. e.g. Sex
class: class of the category "Name". e.g. Male
row.names: subjects. e.g. Josh, Mary

Instead, we shall

write.matrix("Matrix". sep = ", ")

When you do this, we can recover the file with

M = read.csv("Matrix", header = FALSE)

M extracted this way, however, is data.frame.

its columns are given the name Vk, and
its rows are enumerated.

To extract the column, simply call M$Vk.
To extract the row, as.numeric(M[k, ]) will do.
You might want to prepare a separate matrix into which you copy thes numerics.

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?