Save a matrix in to a file using R

2019-07-31 00:40发布

I am quite new to R and I would appreciate any help.

I need to calculate eigenvalues of a series of matrices and then save them in a separate file. My data has 5 columns and 10,000 rows. To be able to calculate this, I need to separate the data into a series of 5 x 5 matrices. As an example please consider a data set with only 5 columns and 10 rows:

1 2 3 4 5
11 21 31 41 51
12 22 32 42 52
13 23 33 43 53
14 24 34 44 54
15 25 35 45 55
16 26 36 46 56
17 27 37 47 57
18 28 38 48 58
19 29 39 49 59 

I have written the code below so far:

R<-NULL
A <- setwd("c:/location of the file on this computer")
for (i in 0:1){
  X <- read.table(A, skip=i*5, nrow=5)
  M <- as.matrix(X)
  E <- eigen(M)
  R<-rbind(R,E)}
}

The results should look something like: eigen() decomposition

    $`values`
    [1]  2.362320e+02+0.000000e+00i -4.960046e+01+1.258757e+01i -4.960046e+01-1.258757e+01i  9.689475e-01+0.000000e+00i
    [5]  1.104994e-14+0.000000e+00i

$vectors
             [,1]                    [,2]                    [,3]           [,4]             [,5]
[1,] 0.9351696+0i  0.95959917+0.00000000i  0.95959917+0.00000000i  0.05003956+0i -1.529602e-15+0i
[2,] 0.1382999+0i -0.07952624-0.04585480i -0.07952624+0.04585480i -0.00162525+0i  4.670542e-17+0i
[3,] 0.1451493+0i -0.09392247-0.04970605i -0.09392247+0.04970605i -0.21235137+0i -4.082483e-01+0i
[4,] 0.2521091+0i  0.11157105+0.16033279i  0.11157105-0.16033279i -0.70990185+0i  8.164966e-01+0i
[5,] 0.1473217+0i -0.13518414-0.05496162i -0.13518414+0.05496162i  0.66965637+0i -4.082483e-01+0i

However, the result I get from the current codes are:

> class(R)
[1] "matrix"
> print(R)
  values    vectors   
E Complex,5 Complex,25
E Complex,5 Complex,25

I have a few questions and it would be a big help if you could help with any of them:

  1. How to fix my problem?

  2. Also, the output Excel file - the one created in the temporary folder - does not open in Excel.

标签: r excel matrix
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-31 00:52

1) Above functions return the following error: cannot open file 'C:/location of the file on this computer': Permission denied.

I simply move the file to a directory which has the access permission, or copy the data of the files in to another file. And finally, import the data using RStudio feature.

2) How can I save the results of a loop into a separate file? In this case, an Excel file with 5 columns and 2 rows (5 eigenvalues for each 5 x 5 matrix).

library(xlsx)
file <- paste(tempdir(), "./usarrests.xlsx", sep="")
res <- write.xlsx(USArrests, file) 

The file shows you the path to find your xlsx file. In my case it is:

"C:\\Users\\salman\\AppData\\Local\\Temp\\Rtmpaskn5E./usarrests.xlsx"

Svaing a matrix is quit the same, you just need to pass your matrix instead of USArrests

https://www.statmethods.net/input/exportingdata.html


How to use it in your case?

A <- setwd("c:/location of the file on this computer")
res<-NULL
for (i in 0:1){
  X <- read.table(A, skip=5, nrow=5)
  M <- as.matrix(X)
  E <- eigen(M)
  res<-rbind(res,E)  # This should aggregate all your Es into a dataframe
}
    class(res)
    print(res) 
    library(xlsx)
    file <- paste(tempdir(), "./eigens.xlsx", sep="")
    res <- write.xlsx(res, file)              # Save E into the file
查看更多
登录 后发表回答