在过多的列中输出嵌套循环

时间:2017-09-06 08:34:14

标签: r for-loop indexing nested-loops

我是循环的新手,我正在努力格式化我的输出。我试图在不同时间点(i)修改物种丰度的时间序列,对于多个物种(k)以各种大小(j)。对于这些选项中的每一个,我想要一个随时间显示丰富度的列。

我编写了一个循环,当我手动输入i,j和k的各种值时(即我得到具有正确值的单列),但是我无法弄清楚如何正确地索引输出矩阵。

虚拟数据集看起来像这样(其中x和y是不同的物种,样本数是时间):

dat<- data.frame(x = sample(1:100, 100, replace = TRUE), y = sample(1:100, 100, replace = TRUE))

对于我的循环,我还创建了一些其他对象:

length <- nrow(dat)
change <- as.matrix(seq(0.0,0.99,0.2))
change.length <- nrow(change)

和填充的最终矩阵(具有正确的尺寸)。 -40是因为我没有修改时间序列中的前20个或后20个丰度

final_matrix <- array(0,c(length,(((length-40)*change.length)*2))) # 2 is the number of species in this example

循环如下:

for(i in 1:(length-40)) {
  for(j in 1:change.length){
    for(k in 1:2){
timestep1 <- dat[0:(19+(i)),k]   # selecting rows that will not be modified based on min + i for a given species k
timestep2 <- dat[(20+i):(length),k] # selecting rows that will be modified for any given species k (columns)

 result1 <- timestep1*1 # not making any changes to the abundance data
 result2 <- timestep2*change[j,1] # multiplying abundance by change (j)

 resultloop<-c(result1,result2) # binding the two matrices into a single column with 100 rows

 final_matrix[,i*j*k] <- resultloop
}}}

按原样索引的最终矩阵产生的列数不正确,结果很奇怪,例如全零的列。

如何索引此矩阵,以便为i,j和k的每个值索引每列(100行代表随时间的丰度)?

编辑:一个示例(模拟)虚拟数据集,用于说明我希望输出看起来像什么:

dat<- data.frame(Spx_I1_j1 = sample(1:100, 100, replace = TRUE),
             Spx_I2_j1 = sample(1:100, 100, replace = TRUE),
             Spx_I3_j1 = sample(1:100, 100, replace = TRUE),
             # etc...
             Spx_I1_k2 = sample(1:100, 100, replace = TRUE),
             Spx_I2_k2 = sample(1:100, 100, replace = TRUE),
             #etc...
             Spy_I1_k1 = sample(1:100, 100, replace = TRUE),
             Spy_I2_k1 = sample(1:100, 100, replace = TRUE))

对于每个物种(k)的每个幅度变化(j),每个修改的时间步长序列(i)的各列表示100个丰度值的每列。

非常感谢您提供有关此事的任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

我不太确定你在问什么。这是我的解释。 顺便提一下,请查看How to make a great R reproducible example?以便将来重新提出您的问题。

#Make some sample data, 10 observations, ignore the first 2 and last 2

ss<-function(x,n){set.seed(x); sample(1:100,n,replace=TRUE)}
dat<-as.matrix(data.frame("Dog"=ss(1,10),"Cat"=ss(2,10))) #Note, make this a matrix!
ind_ignore<-c(1:2,9:10) #these will be ignored, like your first and last x abundances
change <- seq(0.0,0.99,0.2) #5 values, so the expected output is expected to have a dimension of 10 obs by (2 species * 5 changes)
result <- matrix(NA,ncol=ncol(dat)*length(change), nrow=nrow(dat))
colnames(result)<-paste0("V",1:ncol(result))
n_counter<-ncol(dat)-1
counter<-1

> change
[1] 0.0 0.2 0.4 0.6 0.8
> dat
      Dog Cat
 [1,]  27  19
 [2,]  38  71
 [3,]  58  58
 [4,]  91  17
 [5,]  21  95
 [6,]  90  95
 [7,]  95  13
 [8,]  67  84
 [9,]  63  47
[10,]   7  55

然后,我只是将dat值分配给结果矩阵,并更改组合名称。

for(magnitude in change){
  #Indexes of columns in result that should be modified
  ind_col<-counter:(counter+n_counter)

  #Change names
  colnames(result)[ind_col]<-paste0(colnames(dat),"_",magnitude)

  #Apply the change and assign it to the result
  result[-ind_ignore,ind_col]<-dat[-ind_ignore,]*magnitude
  result[ind_ignore,ind_col]<-dat[ind_ignore,]

  counter<-counter+ncol(dat)
}

输出

> result
      Dog_0 Cat_0 Dog_0.2 Cat_0.2 Dog_0.4 Cat_0.4 Dog_0.6 Cat_0.6 Dog_0.8 Cat_0.8
 [1,]    27    19    27.0    19.0    27.0    19.0    27.0    19.0    27.0    19.0
 [2,]    38    71    38.0    71.0    38.0    71.0    38.0    71.0    38.0    71.0
 [3,]     0     0    11.6    11.6    23.2    23.2    34.8    34.8    46.4    46.4
 [4,]     0     0    18.2     3.4    36.4     6.8    54.6    10.2    72.8    13.6
 [5,]     0     0     4.2    19.0     8.4    38.0    12.6    57.0    16.8    76.0
 [6,]     0     0    18.0    19.0    36.0    38.0    54.0    57.0    72.0    76.0
 [7,]     0     0    19.0     2.6    38.0     5.2    57.0     7.8    76.0    10.4
 [8,]     0     0    13.4    16.8    26.8    33.6    40.2    50.4    53.6    67.2
 [9,]    63    47    63.0    47.0    63.0    47.0    63.0    47.0    63.0    47.0
[10,]     7    55     7.0    55.0     7.0    55.0     7.0    55.0     7.0    55.0

编辑

我再也不知道我是否理解了这些评论,这里什么都没有:

ss<-function(x,n){set.seed(x); sample(1:100,n,replace=TRUE)}
dat<-as.matrix(data.frame("Dog"=ss(1,10),"Cat"=ss(2,10))) #Note, make this a matrix!
ignore<-2 #these will be ignored, like your first and last x abundances
change <- seq(0.0,0.99,0.2) #5 values, so the expected output is expected to have a dimension of 10 obs by (2 species * 5 changes * (10-4 ignored points) timepoints)
n_timepoints<-nrow(dat) - ignore*2
result <- matrix(NA,ncol=ncol(dat)*length(change)*n_timepoints, nrow=nrow(dat))
colnames(result)<-paste0("V",1:ncol(result))
n_counter<-ncol(dat)*n_timepoints-1
counter<-1

for(magnitude in change){
  #Indexes of columns in result that should be modified
  ind_col<-counter:(counter+n_counter)

  #Change names
  colnames(result)[ind_col]<-paste0(colnames(dat),"_",magnitude,"_",rep(1:n_timepoints, each=ncol(dat)))

  for (timepoint in 1:n_timepoints){
    #Apply the change and assign it to the result
    ind_col_timepoint <- counter:(counter+ncol(dat)-1)
    ind_ignore_timepoint<-c(1:(ignore+timepoint-1), (nrow(dat)-ignore+1):nrow(dat))
    result[-ind_ignore_timepoint,ind_col_timepoint]<-dat[-ind_ignore_timepoint,]*magnitude
    result[ind_ignore_timepoint,ind_col_timepoint]<-dat[ind_ignore_timepoint,]
    counter<-counter+ncol(dat)
  }

}