R 循环:如何保存输出? [英] Loop in R: how to save the outputs?

查看:43
本文介绍了R 循环:如何保存输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从逻辑测试循环中保存数据.

I am trying to save the data from a loop of logical tests.

所以我有以下数据:

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

我已经运行了以下循环

for (i in 1:49) {
  print(T1 > F[i]) # I used print to see the results in the screen
}

这个循环返回 49 个填充有逻辑值(真或假)的矩阵.每个矩阵是 T1 与 49 个位置 F[i] (F[1], ...., F[49]) 中每一个的比较

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])

我需要将这些矩阵中的值进一步用作网络图的邻接矩阵.但是,当我既不能将这些逻辑值分配给矩阵时,也不能使用write.matrix"将它们保存在 csv 值中.

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".

所以,我需要用逻辑值(T 或 F)填充 49 个矩阵W".我已经通过上面的循环获得了这些值,但我无法将其作为对象或 csv 的集合.文件.

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.

我试过了

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
}

返回以下警告

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
  }
}

并比较两个输出

Loop.T1(T1)>create.M(F)

哪个返回

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆