在R循环:如何保存输出? [英] Loop in R: how to save the outputs?
问题描述
所以我有以下数据:
T1 < - 矩阵(seq(from = 100000,to = 6600000,length.out = 676),26,26)#26X26的矩阵 -
minmax < - seq(从= 1到= 49,by = 1)#创建序列
Fstep <-6569141.82 / minmax#定义从0到6569141.82的向量, 49个部门
F < - rev(round(Fstep,0))#整理矢量值并重新命令它们
F
我已经运行了下面的循环:
{
print(T1> F [i])#我用print来查看屏幕上的结果
}
这个循环返回给我49个填充了逻辑值(True或False)的矩阵。每个矩阵是T1与49个位置F [i](F [1],...,F [49])中每一个的比较。我需要使这些矩阵中的值进一步用作网络图的邻接矩阵。但是,当我既不能将这些逻辑值分配给矩阵,也不使用write.matrix将它们保存在csv值中。
因此,我需要用逻辑值(T或F)填充49个矩阵W。我已经通过上面的循环得到了这些值,但我不能把它作为一个对象或csv的集合。文件。
我试过了
$ $ $ $ $ $ $ c $ W $ -matrix(0, 26,26)#创建一个空矩阵来为(i在1:49)中赋值逻辑参数
{
W [i] < - T1> F [i]#我用print来看在
}
这样的结果中返回下面的警告:
pre $警告信息
1:在W [i] < - (T1> F [i]):
号码的项目,以取代不是更换长度的倍数
我也尝试了不同的设置,其中所有的我比较的矩阵具有相同的尺寸。
create.M < - function(F){#将函数把每个位置F [i]变换成26X26矩阵
for(i in 1:49){
matrix(F [i],26,26)
}
}
Loop.T1< ;函数(T1){#一个函数复制T1(49次)
for(i in 1:49){
T1
}
}
并比较了两个输出
Loop.T1(T1)> create.M(F)
p>
逻辑(0)
将每个布尔矩阵存储为列表中的一个项目:结果< - vector(list,49)
for(i in 1:49)
{
result [[i]] <-T1> F [i]#I使用打印在屏幕上查看结果
#在屏幕上打印第一个矩阵
结果[[1]]
I am trying to save the data from a loop of logical tests.
So I have the following data:
T1 <- matrix(seq(from=100000, to=6600000,length.out=676),26,26) # a matrix of 26X26 - here with illustrive values
minmax <- seq(from=1,to=49,by=1) # creates a sequence
Fstep <- 6569141.82/minmax # define a vector from 0 to 6569141.82 with 49 divisions
F <- rev(round(Fstep,0)) # round the vector values and re order them
F
I have runned the following loop
for (i in 1:49) {
print(T1 > F[i]) # I used print to see the results in the screen
}
This loop returns me 49 matrices filled in with logical values (True or false). Each matrix is the comparison of T1 against each of the 49 positions F[i] (F[1], ...., F[49])
I need to have the values in those matrices for further using as adjacency matrices for network plots. However when I can't neither assign those logical values to an matrix, neither save them in csv values using "write.matrix".
So, I need to have 49 - matrices "W" filled in with logical values (T or F). I already got those values by the loop above but I can't get it as an object or as collection of csv. files.
I tried
W<-matrix(0,26,26) #create an empty matrix to assign the logical arguments
for (i in 1:49){
W[i] <- T1>F[i] # I used print to see the results in the screen
}
which returns the following warning
Warning messages:
1: In W[i] <- (T1 > F[i]) :
number of items to replace is not a multiple of replacement length
I also tried a different setting in which all the matrices I compare have the same dimensions.
create.M <- function(F){ # a function to transform each position F[i] into a 26X26 matrix
for (i in 1:49) {
matrix(F[i],26,26)
}
}
Loop.T1 <- function(T1){ # a function to replicate T1(49 times)
for ( i in 1:49) {
T1
}
}
and compared the two outputs
Loop.T1(T1)>create.M(F)
which returns
logical(0)
Store each boolean matrix as an item in a list:
result <- vector("list",49)
for (i in 1:49)
{
result[[i]] <- T1>F[i] # I used print to see the results in the screen
}
#Print the first matrix on screen
result[[1]]
这篇关于在R循环:如何保存输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!