如何运行所有" N取2"迭代的一个函数?

时间:2017-05-12 03:55:00

标签: r

我写过一个简单的相关函数,它接受三个变量。 " A"和" B"是等长的数值向量," n"是长度。

Corr.fxn <- function(A, B, n){
Correlation <- (sum((A - mean(A))*(B - mean(B))) / (n-1)) / (sd(A)*sd(B))
return(Correlation)
}

该功能运行良好,但我有许多我想要处理的向量。修改此代码以处理所有&#34; N取2&#34;的最佳方法是什么?对我的矢量&#34; N&#34;?

的独特分析

编辑:

显示载体结构的示例数据:

A <- c(-1, 0, 1, -1, 0, 1, -1, 0, 1)
B <- c(1, 1, -1, 0, 1, -1, 0, 0, 1)
...
n <- length(A)

所以,让我说我有矢量A到Z,我想修改我的代码,输出一个包含所有{26取2}相关值的新矢量。

2 个答案:

答案 0 :(得分:1)

假设列表v中有一堆数字向量,可以采用以下一种方法来执行此操作:

v <- list()
for (i in 1:10) {
  v[[i]] <- sample(1:10, 10, replace = TRUE)
}

apply(combn(1:10, 2), 2, function(x) Corr.fxn(v[[x[1]]], v[[x[2]]], length(v[[x[1]]])))

答案 1 :(得分:0)

在这个答案中,我假设有两件事。首先,您想自己编写一个函数,否则您可以使用import random def randIntArray(): randomList = [] randomList = random.sample(range(1, 101), 10) return randomList def getInt(prompt): # Function to call when asking for the A and B values later. try: x = input(prompt) y = int(x) return y except: print("That was not an integer. Please enter an integer. ") def getData(): userIntList = [] for i in range(10): userNum = getInt("Please enter value number : ") userIntList.append(userNum) return userIntList def swap(a, j, k): temp1 = a[j] a[j] = a[k] a[k] = temp1 def sortArray(a): for i in range(len(a)): for k in range(len(a) - 1): first = k second = k + 1 if (a[first] > a[second]): # Uses Swap function swap(a, first, second) def displayArray(theArray): outList = theArray return outList def maxValue(theArray): maxNum = 0 for i in theArray: if i > maxNum: maxNum = i return maxNum def minValue(theArray): minNum = theArray[0] for i in theArray: if i < minNum: minNum = i return minNum def totalValue(theArray): aTotal = 0 for s in theArray: aTotal += s return aTotal def aveValue(theArray): aTotal = totalValue(theArray) aAverage = aTotal / len(theArray) return aAverage def dataOut(theArray): print("The maximum value is: " + str(maxValue(theArray))) print("The minimum value is: " + str(minValue(theArray))) print("The total value is: " + str(totalValue(theArray))) print("The average value is: " + str(aveValue(theArray)) + "\n") def processInput(theArray): print("The original array was:\n " + str(displayArray(theArray)) + "\n") dataOut(theArray) sortArray(theArray) print("The sorted array is:\n " + str(displayArray(theArray))) def unitTest(): print("Running test . . .") randIntArray() theArray = randIntArray() processInput(theArray) print("\n Finished test . . .") def main(): unitTest() theArray = [] done = False while not done: proceedQ = input("Would you like to enter 10 numbers <y/n>? ") if proceedQ == "y": getData() processInput(theArray) if proceedQ != "y": done = True main() 。其次,你想要&#34; N取2&#34;部分在函数内部,否则前面建议的方法是正确的。在这种情况下,您可以这样做:

Hmisc::rcorr

Corr.fxn <- function(vectors, n){ pairs<- combn(length(vectors), 2) npairs<- ncol(pairs) cor.mat<- matrix(NA, nrow = length(vectors), ncol = npairs) for (i in 1:ncol(pairs)){ A<- vectors[[pairs[1, i]]] B<- vectors[[pairs[2, i]]] cor.mat[pairs[1, i], pairs[2, i]] <- (sum((A - mean(A))*(B - mean(B))) / (n-1)) /(sd(A)*sd(B)) } cor.mat[lower.tri(cor.mat)]<- cor.mat[upper.tri(cor.mat)] ### diag(cor.mat)<- 1 ### cor.mat<- data.frame(cor.mat) ### row.names(cor.mat)<- colnames(cor.mat)<- names(vectors) ### return(cor.mat) } 结尾的行是出于装饰原因。主要输入是一个名为&#34; vectors&#34;的列表。所以它的工作原理如下:

###