使用R将矩阵保存到文件中 [英] Save a matrix in to a file using R
问题描述
我对R还是很陌生,我将不胜感激.
I am quite new to R and I would appreciate any help.
我需要计算一系列矩阵的特征值,然后将它们保存在单独的文件中.我的数据有5列和10,000行.为了能够计算出该值,我需要将数据分成一系列5 x 5矩阵.例如,请考虑一个只有5列和10行的数据集:
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)}
}
结果应类似于: eigen()分解
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:
-
如何解决我的问题?
How to fix my problem?
此外,输出Excel文件(在临时文件夹中创建的文件)不会在Excel中打开.
Also, the output Excel file - the one created in the temporary folder - does not open in Excel.
推荐答案
1)上面的函数返回以下错误:无法打开文件 "C:/该文件在此计算机上的位置":权限被拒绝.
1) Above functions return the following error: cannot open file 'C:/location of the file on this computer': Permission denied.
我只是将文件移动到具有访问权限的目录中,或者将文件的数据复制到另一个文件中.最后,使用RStudio功能导入数据.
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)如何将循环结果保存到单独的文件中?在这个 情况,一个Excel文件有5列2行(每个都有5个特征值) 5 x 5矩阵).
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)
file
向您显示找到xlsx文件的路径.以我为例:
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"
保存矩阵是完全一样的,您只需要传递矩阵而不是USArrests
Svaing a matrix is quit the same, you just need to pass your matrix instead of USArrests
https://www.statmethods.net/input/exportingdata.html
如何在您的情况下使用它?
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
这篇关于使用R将矩阵保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!