本文介绍了R:枚举矩阵的列组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! (编辑注:我将标题改为R:枚举矩阵的列组合,从R grep:匹配字符串矩阵到列表)以更好地反映解决方案) 我试图将一个字符串矩阵匹配到一个列表中:这样我就可以最终在 data.frame 。 第一部分按预期工作,返回所有可能的配对,三元组和四元组的列表(尽管也许这种方法创建了我的绑定?): priceList ccc = rnorm(100,100,10),ddd = rnorm(100,100,10), eee = rnorm(100,100,10),fff = rnorm(100,100,10) 10), ggg = rnorm(100,100,10)) getTrades< - 函数(dd,Maxleg = 3) {节点< - colnames(dd) tradeList< - list() for(i in 2:Maxleg){ tradeLeg< - paste0('legs',i) tradeList [[tradeLeg]] } return(tradeList)} tradeCombos< - getTrades(priceList,4) 现在我想把这个可能的组合列表变成交易。例如: > tradeCombos [[1]] [,1] [1]aaabbb 成为 priceList [,2] - priceList [,1] 等等。 我已经尝试了一些使用 grep 和类似命令的方法,并且觉得我已经接近以下内容: pre $ LocList< - sapply(tradeCombos [[1]],regexpr,colnames(priceList)) 然而这种格式并不适合下一步。 理想情况下, LocList [1] 会返回类似于: 1 2 $ b 假设 tradeCombos [[1]] [,1] ==aaabbb。 有人可以帮忙吗? __ 在下面所有答案的帮助下,我现在得到了: colDiff< - function(x) { Reduce(' - ',rev(x)) $ b $ getTrades< - function(dd,Maxleg = 3) { tradeList< - list() for(i in 2:Maxleg ){ tradeLeg< - paste0('legs',i) tradeLegsList< - combn(names(dd),i, function(x)dd [x] FALSE) nameMtx< - combn(names(dd),i) names(tradeLegsList)< - apply(nameMtx,MARGIN = 2, FUN = function(x)paste rev(x),collapse ='*')) tradeList [[tradeLeg]]< - lapply(tradeLegsList,colDiff)} return(tradeList)} tradeCombos< - getTrades(priceList,4) 的构成部分,是我试图实现的一切。 非常感谢所有人的帮助。 解决方案使用 lapply , apply 和 Reduce 。 bpp(tradeCombos, function(combos) apply(combos,MARGIN = 2,FUN = function(combo)Reduce(' - ',priceList [rev(combo)]))) combo 是一个组合矩阵 tradeCombos 。 rev(combo)反转列,使最后一个值为第一个值。用于从 data.frame 中选择列子集的 R 语法为 DF [col 。 priceList ,所以 priceList [rev(combo)] 是只有 combo 中的列以相反的顺序排列。 data.frame s实际上只是 list 的列,所以任何函数都可以遍历 list s遍历 data.frame 中的列。 Reduce 就是这样一个函数。 Reduce 接受一个函数(在这种情况下,减法函数 - )和一个 list ($($)code>),然后用前一次调用的结果连续调用列表中的参数的函数,例如(((arg1 - arg2) - 您可以重命名 tradeCombos 中的列,以便最终列名反映它们的源代码。 : tradeCombos 函数(组合){ dimnames(combos) [[2]]< - apply(combos, MARGIN = 2, FUN = function(combo)paste(rev(combo),collapse =' - ')) return(combos)} ) (edit note: I changed the Title to "R: enumerate column combinations of a matrix", from "R grep: matching a matrix of strings to a list" to better reflect the solution)I am trying to match a matrix of strings to a list: so that i can ultimately use the matrix as a map in later operations on a data.frame.This first part works as intended, returning a list of all the possible pairs, triples and quad combinations (though perhaps this approach has created my bind?): priceList <- data.frame(aaa = rnorm(100, 100, 10), bbb = rnorm(100, 100, 10), ccc = rnorm(100, 100, 10), ddd = rnorm(100, 100, 10), eee = rnorm(100, 100, 10), fff = rnorm(100, 100, 10), ggg = rnorm(100, 100, 10))getTrades <- function(dd, Maxleg=3){ nodes <- colnames(dd) tradeList <- list() for (i in 2:Maxleg){ tradeLeg <- paste0('legs',i) tradeList[[tradeLeg]] <- combn(nodes, i) } return(tradeList)}tradeCombos <- getTrades(priceList, 4)I'd now like to turn this list of possible combinations into trades. For example: > tradeCombos[[1]][,1][1] "aaa" "bbb"Needs to eventually become priceList[,2] - priceList[,1], and so forth.I have tried a few approaches with grep and similar commands, and feel that i've come close with the following: LocList <- sapply(tradeCombos[[1]], regexpr, colnames(priceList))However the format is not quite suitable for the next step.Ideally, LocList[1] would return something like: 1 2Assuming that the tradeCombos[[1]][,1] == "aaa" "bbb".Can someone please help?__With help from all of the answers below, i've now got: colDiff <- function(x){ Reduce('-', rev(x))}getTrades <- function(dd, Maxleg=3){ tradeList <- list() for (i in 2:Maxleg){ tradeLeg <- paste0('legs',i) tradeLegsList <- combn(names(dd), i, function(x) dd[x], simplify = FALSE) nameMtx <- combn(names(dd), i) names(tradeLegsList) <- apply(nameMtx, MARGIN=2, FUN=function(x) paste(rev(x), collapse='*')) tradeList[[tradeLeg]] <- lapply(tradeLegsList, colDiff) } return(tradeList)}tradeCombos <- getTrades(priceList, 4)This retains the names of the constitutent parts, and is everything I was trying to achieve.Many thanks to all for the help. 解决方案 This gets your eventual aim using lapply, apply, and Reduce.lapply(tradeCombos, function(combos) apply(combos, MARGIN=2, FUN=function(combo) Reduce('-', priceList[rev(combo)])))combo is a column from one of the combo matrices in tradeCombos. rev(combo) reverses the column so the last value is first. The R syntax for selecting a subset of columns from a data.frame is DF[col.names], so priceList[rev(combo)] is a subset of priceList with just the columns in combo, in reverse order. data.frames are actually just lists of columns, so any function that's designed to iterate over lists can be used to iterate over the columns in a data.frame. Reduce is one such function. Reduce takes a function (in this case the subtract function -) and a list of arguments and then successively calls the function on the arguments in the list with the results of the previous call, e.g., (((arg1 - arg2) - arg3) - arg4).You rename the columns in tradeCombos so that the final column names reflect their source with:tradeCombos <- lapply(tradeCombos, function(combos) { dimnames(combos)[[2]] <- apply(combos, MARGIN=2, FUN=function(combo) paste(rev(combo), collapse='-') ) return(combos) }) 这篇关于R:枚举矩阵的列组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 01:31