gpt4 book ai didi

r - 循环输入R : how to save the outputs?

转载 作者:行者123 更新时间:2023-12-04 10:05:10 26 4
gpt4 key购买 nike

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

所以我有以下数据:

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
}

这个循环返回了我用逻辑值(True或false)填充的49个矩阵。每个矩阵都是T1与49个位置F [i](F [1],....,F [49])中每个位置的比较

我需要在这些矩阵中具有值,以便进一步用作网络图的邻接矩阵。但是,当我既无法将这些逻辑值分配给矩阵时,也无法使用“write.matrix”将其保存为csv值。

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

我试过了
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

我还尝试了一种不同的设置,在该设置中,我比较的所有矩阵都具有相同的维数。
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)

最佳答案

将每个 bool 矩阵存储为列表中的一项:

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 - 循环输入R : how to save the outputs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9689387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com