本文介绍了如何在R中创建一个for循环,用于这个特殊的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 给出这两个不同的数据框 df(A)(nrow = 10,ncol = 3)和 df(B)(nrow = 3,ncol = 3) df(A)df(B) col1 col2 col3 col1 col2 col3 1 2 4 1 4 5 3 5 7 2 7 7 5 7 6 3 9 8 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 我想: p> 将df(A)中的列的每个值与对应列的df(B)乘以 让我举个例子: A [1,1] * B [1,1] + A [2,1] * B [2,1] + A [3,1] * B [3,1] = 1 * 1 + 3 * 2 + 5 * 3 = 22#第一个预期结果 A [2,1] * B [1,1] + A [3,1] * B [2,1] + A [4,1] * B [3,1] = 3 * 1 + 5 * 2 + 6 * 3 = 31#第二预期结果 A [3,1] * B [1,1] + A [ 4,1] * B [2,1] + A [5,1] * B [3,1] = 5 * 1 + 6 * 2 + 9 * 3 = 44#第三个预期结果 ... A [8,1] * B [1,1] + A [9,1] * B [2,1] + A [10,1] * B [3,1] = 3.5 * 1 + 6 * 2 + 67 * 3 = 216.5#最后预期结果 等等从每个值每列 df(A),直到最后一个可能的三元组。 我尝试使用为循环代码: temp< - rep(NA,3) my_matrix< - 矩阵(0,ncol = ncol(A),nrow = nrow(A)对于(i in 1:nrow(A))中的 { for(j in 1:3){ temp(j)< - A [i + 1,] * B [i + j-1,] } my_matrix(i)< - sum(temp)} / pre> 但R回复 总和错误(temp) :参数无效'type'(list) 另外:有3个警告(使用warnings()来查看它们) >警告() 1:1:dim(A):数字表达式有2个元素:只有第一个使用 2:In temp [j] 要替换的项目数量不是替换的倍数长度 提前谢谢最佳 Anna 解决方案除了user164385的答案,这是非常好的,您还可以使用以下循环会做这个伎俩(即使它们并不总是最佳的,循环可以使得从R开始时更容易)。请注意,使用全数字数据,您可以使用矩阵而不是数据框: 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 结果< - 矩阵(nrow = 8,ncol = 3) for(i in 1: (nrow(A)-2)){ results [i,]< - colSums(A [i:(i + 2),] * B)} results [,1] [,2] [,3] [1,] 22.0 106.0 117.0 [2,] 31.0 150.0 124.2 [3,] 44.0 190.0 135.3 [4,] 37.5 162.5 148.7 [5,] 81.0 142.8 204.1 [6,] 57.0 113.9 182.7 [7,] 46.0 132.9 118.0 [8,] 216.5 111.3 53.0 I can't figure out with this (apparently) simply kind of operation:Given these two diferent dataframes df(A) (nrow=10,ncol=3) and df(B) (nrow=3,ncol=3)df(A) df(B) col1 col2 col3 col1 col2 col3 1 2 4 1 4 53 5 7 2 7 75 7 6 3 9 86 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 2I'd like to:multiply each value of the columns in df(A) with those of df(B) of the correspondant columnand than sum three consecutive results starting from every row.Let me give you an example: A[1,1]*B[1,1] + A[2,1]*B[2,1] + A[3,1]*B[3,1]= 1*1+3*2+5*3= 22 # first expected result A[2,1]*B[1,1] + A[3,1]*B[2,1] + A[4,1]*B[3,1]= 3*1+5*2+6*3 = 31 # second expected resultA[3,1]*B[1,1] + A[4,1]*B[2,1] + A[5,1]*B[3,1]= 5*1+6*2+9*3 = 44 # third expected result ...A[8,1]*B[1,1] + A[9,1]*B[2,1] + A[10,1]*B[3,1]= 3.5*1+6*2+67*3 = 216.5 # last expected resultand so on starting from each value of each column of df(A) until the last possible triplet. I tried with this for loop code:temp <- rep(NA,3)my_matrix <- matrix(0,ncol=ncol(A),nrow=nrow(A))for (i in 1:nrow(A)){ for (j in 1:3){ temp(j) <- A[i+j-1,]*B[i+j-1,] } my_matrix(i) <- sum(temp)}but R repliesError in sum(temp) : invalid 'type' (list) of argumentIn addition: There were 3 warnings (use warnings() to see them)> warnings()1: In 1:dim(A) : numerical expression has 2 elements: only the first used2: In temp[j] <- A[i + (j - 1), ] * B[i + (j - 1), ] : number of items to replace is not a multiple of replacement lengthThank you in advanceBestAnna 解决方案 As an alternative to user164385's answer, which is very good, you can also use the following loop that will do the trick (even though they're not always optimal, loops can make it easier when beginning with R). Note that with all-numeric data, you can use matrices instead of dataframes: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)B <- matrix(c(1,4,5,2,7,7,3,9,8), ncol=3, byrow=T)results <- matrix(nrow=8, ncol=3)for(i in 1:(nrow(A)-2)) { results[i,] <- colSums(A[i:(i+2),] * B)}results [,1] [,2] [,3][1,] 22.0 106.0 117.0[2,] 31.0 150.0 124.2[3,] 44.0 190.0 135.3[4,] 37.5 162.5 148.7[5,] 81.0 142.8 204.1[6,] 57.0 113.9 182.7[7,] 46.0 132.9 118.0[8,] 216.5 111.3 53.0 这篇关于如何在R中创建一个for循环,用于这个特殊的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 18:15