如何使用(仅)for-loop进行此操作? [英] How use (only) for-loop for this operation?

查看:162
本文介绍了如何使用(仅)for-loop进行此操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个矩阵:

pre $ A < - 矩阵(c(1,2,4,3,5, 7,5,7,6,6,9,
5.9,9,11,8,4.5,5.5,7.9,
21,6.7,13.6,3.5,5,6,6,
7.9,1,67,4,2),ncol = 3,byrow = T)

和这个矢量:

  B < -  c(2,3,4)





$ p $ X1 X2 X3
[1,] 4.0 14.0 22.9
[2,] 8.0 21.0 26.9
[3,] 11.0 27.0 27.8
[4,] 15.0 25.5 35.4
[5] 13.5 23.2 35.5
[6,] 25.5 17.2 28.5
[7,] 24.5 19.6 22.6
[8,] 9.5 16.9 NA
[9,] 73.0不适用NA
[10,]不适用NA

#最终代码下的操作是:
col 1:
A [1,1] + A [2 ,1] = 1 + 3 = 4#第一列的结果。
A [2,1] + A [3,1] = 3 + 5 = 8#第二列结果为
。 。 。
A [9,1] + A [10,1] = 6 + 67 = 73#首列预期结果

col 2:
A [1,2 ] + A [2,2] + A [3,2] = 2 + 5 + 7 = 14#第一列结果为第二列。第二列

A [2,2] + A [3,2] + A [4,2] = 5 + 7 + 9 = 21#秒。 。 。
A [8,2] + A [9,2] + A [10,2] = 5 + 7.9 + 4 = 16.9 #last第二列预期结果
依此类推矩阵A相应的向量B的值。

我试过这段代码:



$ p $ res $ lt; code> res < - data.frame(matrix(ncol = ncol(A),nrow = nrow(A)))
res_tot (1:ncol(A)){
for(t in 1:ncol(A))
(A)){
res< -A [t + B [j],j]
res_tot [t,j]< - res
}
}

但索引不正确。现在,如果可能的话,我会只用代码循环和使用索引(如 i,j,k ...)没有像mapply,rollSum等功能从包。
是否有可能?请...

解决方案

我会运行 sapply 和一个循环。当然,你也可以使用一个嵌套的 sapply 函数。

  res <  -  NULL 
for(j in 1:3){
tmp< - sapply(1:nrow(A),function(i)ifelse((i + j> ,NA,sum(A [i:(i + j),j])))
res< -Cbind(res,tmp); colnames(res)< - NULL
}
res
[,1] [,2] [,3]
[1,] 4.0 14.0 22.9
[2,] 8.0 21.0 26.9
[3,] 11.0 27.0 27.8
[4,] 15.0 25.5 35.4
[5,] 13.5 23.2 35.5
[6,] 25.5 17.2 28.5
[7,] 24.5 19.6 22.6
[8,] 9.5 16.9 NA
[9,] 73.0 NA NA
[10,]不适用NA

编辑:没有sapply和向量B,定义总计行数:

  B <  -  c(2,3,4)
res< - NULL
for(j in 1:3){
for(i in 1:nrow(A)){$如果(i == 1)tmp2 < - NULL
tmp < - ifelse((i-1 + B [j]> nrow(A)),NA,sum(A [i: (i-1 + B [j]),j]))
tmp2 <-rbind(tmp2,tmp); rownames(tmp2)< - NULL
}
res< - cbind(res,tmp2); colnames(res)< - NULL
}


I have this matrix:

A <- matrix(c(1,2,4,3,5,7,5,7,6,6,9,
              5.9,9,11,8,4.5,5.5,7.9,
              21,6.7,13.6,3.5,5,6,6,
              7.9,1,67,4,2), ncol=3, byrow=T)

and this vector:

B <- c(2 ,3, 4)

I would this expected result:

      X1   X2   X3
 [1,]  4.0 14.0 22.9   
 [2,]  8.0 21.0 26.9
 [3,] 11.0 27.0 27.8
 [4,] 15.0 25.5 35.4
 [5,] 13.5 23.2 35.5
 [6,] 25.5 17.2 28.5
 [7,] 24.5 19.6 22.6
 [8,]  9.5 16.9   NA
 [9,] 73.0   NA   NA
[10,]   NA   NA   NA

# the operation under the eventually code is:
col 1: 
    A[1,1]+A[2,1]=1+3=4 # first result for first column. 
    A[2,1]+A[3,1]=3+5=8 # second result for first column
    . . .   
    A[9,1]+A[10,1]=6+67= 73 #last expected result for first column

col 2 : 
            A[1,2]+A[2,2]+A[3,2]=2+5+7=14 # first result for second column. 
            A[2,2]+A[3,2]+A[4,2]=5+7+9=21 # second result for second column
             . . .
            A[8,2]+A[9,2]+A[10,2]=5+7.9+4=16.9 #last expected result for second column
and so on for the third columns of matrix A accordingly to the values of vector B.

I tried with this code:

res <- data.frame(matrix(ncol=ncol(A),nrow=nrow(A)))
res_tot <- data.frame(matrix(ncol=ncol(A),nrow=nrow(A)))
for (j in 1:ncol(A)){
    for(t in 1:nrow(A)){
      res <- A[t+B[j],j]
       res_tot[t,j] <- res
    }
  }

but the index are not correct. Now, I would, if is possible, the code only with for loop and use of index(like i,j,k etc...) without function like mapply,rollSum, etc.. from packages. Is it possible? please...

解决方案

I would run a combination of sapply and a for loop. Of course you can use a nested sapply function as well.

res <- NULL
for (j in 1:3){
 tmp <-   sapply(1:nrow(A), function(i) ifelse((i+j > nrow(A), NA, sum(A[i:(i+j), j])))
 res <- cbind(res,tmp);colnames(res) <- NULL
}
res
      [,1] [,2] [,3]
 [1,]  4.0 14.0 22.9
 [2,]  8.0 21.0 26.9
 [3,] 11.0 27.0 27.8
 [4,] 15.0 25.5 35.4
 [5,] 13.5 23.2 35.5
 [6,] 25.5 17.2 28.5
 [7,] 24.5 19.6 22.6
 [8,]  9.5 16.9   NA
 [9,] 73.0   NA   NA
[10,]   NA   NA   NA

Edit: without sapply and with vector B, defining number of rows to sum up:

B <- c(2, 3, 4)
res <- NULL
for (j in 1:3){
  for(i in 1:nrow(A)){
  if(i == 1) tmp2 <- NULL
  tmp <-   ifelse(( i-1+B[j] >nrow(A)),NA, sum(A[i:(i-1+B[j]),j])) 
  tmp2 <- rbind(tmp2,tmp);rownames(tmp2) <- NULL
  }
  res <- cbind(res,tmp2);colnames(res) <- NULL
}

这篇关于如何使用(仅)for-loop进行此操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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