- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试最大化横截面面板数据中的数据点数量。我的矩阵结构如下,y 轴为年份,x 轴为国家/地区:
A B C D
2000 NA 50 NA 85
2001 110 75 76 86
2002 120 NA 78 87
2003 130 100 80 88
因此,我试图找到年度数据点的所有可能组合,以获得每个组合最多的国家/地区。使用上面的示例,我尝试生成向量、列表或其他类型的对象,类似于以下内容:
2000, 2001, 2002, 2003 = D
2000, 2001, 2003 = D and B
2001, 2002, 2003 = D, A and C
2000, 2001 = D and B
2001, 2002 = D, A and C
2002, 2003 = D, A and C
2000 = D and B
2001 = A, B, C and D
2002 = A, C and D
2003 = A, B, C and D
这是一件抽象的事情,我无法理解它。我将不胜感激任何帮助。
最佳答案
这是一个很好的起点,但可能还可以改进的解决方案:
library(RcppAlgos)
getCombs <- function(myMat, myCap = NULL, minYears = NULL) {
numRows <- nrow(myMat)
myColNames <- colnames(myMat)
if (is.null(minYears)) ## set default
repZero <- numRows - 1
else if (minYears >= numRows || minYears < 1) ## check for extreme cases
repZero <- numRows - 1
else
repZero <- numRows - minYears
combs <- comboGeneral(v = c(0,1:numRows),
m = numRows, freqs = c(repZero,
rep(1, numRows)), rowCap = myCap)
## I think this part could be improved
out <- lapply(1:nrow(combs), function(x) {
myRows <- myMat[combs[x,],]
if (is.null(nrow(myRows)))
result <- !is.na(myRows)
else
result <- complete.cases(t(myRows))
myColNames[result]
})
myRowNames <- rownames(myMat)
names(out) <- lapply(1:nrow(combs), function(x) myRowNames[combs[x,combs[x,]>0]])
out
}
这是 OP 示例的输出。 (OP 缺少以下 5 个结果):
testMat <- matrix(c(NA, 50, NA, 85, 110, 75, 76, 86, 120, NA, 78, 87, 130, 100, 80, 88), nrow = 4, byrow = TRUE)
row.names(testMat) <- 2000:2003
colnames(testMat) <- LETTERS[1:4]
getCombs(testMat)
$`2000`
[1] "B" "D"
$`2001`
[1] "A" "B" "C" "D"
$`2002`
[1] "A" "C" "D"
$`2003`
[1] "A" "B" "C" "D"
$`c(2000, 2001)`
[1] "B" "D"
$`c(2000, 2002)`
[1] "D"
$`c(2000, 2003)`
[1] "B" "D"
$`c(2001, 2002)`
[1] "A" "C" "D"
$`c(2001, 2003)`
[1] "A" "B" "C" "D"
$`c(2002, 2003)`
[1] "A" "C" "D"
$`c(2000, 2001, 2002)`
[1] "D"
$`c(2000, 2001, 2003)`
[1] "B" "D"
$`c(2000, 2002, 2003)`
[1] "D"
$`c(2001, 2002, 2003)`
[1] "A" "C" "D"
$`c(2000, 2001, 2002, 2003)`
[1] "D"
但是,这个答案或任何 future 的答案不会为您提供所有组合,因为您拥有 144 个国家/地区和 47 年的数据。这会产生一个非常非常的数字。任何长度不超过 n 的每个组合都相当于 power set 。幂集中的元素数量仅为2^n
。由于我们没有计算空集的等价物,因此我们需要减一,因此:
library(gmp)
sub.bigz(pow.bigz(2, 47),1)
Big Integer ('bigz') :
[1] 140737488355327
是的,超过一百万亿!!!您可能需要重新考虑您的方法,因为结果太多。
一切都还没有丢失!您可以使用 myCap
参数来限制结果的数量,以便您仍然可以研究可能的组合。观察:
set.seed(11111)
biggerTest <- matrix(sample(100, 20*20, replace = TRUE), nrow = 20)
library(countrycode)
colnames(biggerTest) <- LETTERS[1:20]
rownames(biggerTest) <- 1988:2007
## set 10% of values to NA
myNAs <- sample(400, 400 / 10)
biggerTest[myNAs] <- NA
biggerTest[1:6, 1:10]
A B C D E F G H I J
1988 51 71 79 35 22 33 22 84 68 4
1989 NA 51 73 10 48 NA 62 44 29 60
1990 NA 21 NA 44 91 24 45 62 52 18
1991 91 91 58 79 65 34 36 87 54 32
1992 82 6 74 75 99 NA 20 28 64 30
1993 80 10 43 100 24 22 99 28 22 44
## Getting all 1,048,575 results takes a good bit of time
system.time(allResults <- getCombs(biggerTest))
user system elapsed
49.449 0.726 50.191
## Using myCap greatly reduces the amount of time
system.time(smallSampTest <- getCombs(biggerTest, myCap = 10000))
user system elapsed
0.252 0.003 0.257
或者,您可以使用 minYears
参数仅返回具有最少年份组合数的结果。例如,根据 OP 对 @CPak 答案的评论,如果您只想查看 15 年或以上组合的结果,我们有:
system.time(minYearTest <- getCombs(biggerTest, minYears = 15))
user system elapsed
1.408 0.018 1.428
set.seed(123)
minYearTest[sample(length(minYearTest), 5)]
$`c(1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2004, 2005, 2007)`
[1] "C" "E" "G" "T"
$`c(1988, 1989, 1990, 1991, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2007)`
[1] "G" "I" "T"
$`c(1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001, 2003, 2004, 2005, 2007)`
[1] "D" "G" "K" "M" "T"
$`c(1988, 1990, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, 2007)`
[1] "G" "J" "K" "T"
$`c(1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2003, 2004, 2005, 2006, 2007)`
[1] "E" "G" "T"
或者同时使用两个参数:
system.time(bothConstraintsTest <- getCombs(biggerTest, 10000, minYears = 10))
user system elapsed
0.487 0.004 0.494
bothConstraintsTest[1:5]
$`c("1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997")`
[1] "E" "G" "H" "J" "M" "R" "T"
$`c("1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1998")`
[1] "E" "G" "H" "J" "T"
$`c("1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1999")`
[1] "D" "E" "G" "M" "T"
$`c("1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "2000")`
[1] "D" "G" "J" "M" "R" "T"
$`c("1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "2001")`
[1] "D" "E" "G" "H" "J" "M" "R" "T"
我们需要做的第一件事是确定n年的每一个组合。这归结为查找 multiset 的所有 n 元组。 c(rep(0, n-1), 1:n)
或等价地,n 元素集的幂集减去空集。例如,对于 2000:2003
年(4 年跨度),可能的组合由下式给出:
comboGeneral(v = c(0,1:4), m = 4,
freqs = c(3, rep(1, 4)))
[,1] [,2] [,3] [,4]
[1,] 0 0 0 1
[2,] 0 0 0 2
[3,] 0 0 0 3
[4,] 0 0 0 4
[5,] 0 0 1 2
[6,] 0 0 1 3
[7,] 0 0 1 4
[8,] 0 0 2 3
[9,] 0 0 2 4
[10,] 0 0 3 4
[11,] 0 1 2 3
[12,] 0 1 2 4
[13,] 0 1 3 4
[14,] 0 2 3 4
[15,] 1 2 3 4
现在,我们迭代组合的每一行,其中每一行告诉我们原始矩阵中的哪些行组合要测试 NA
。如果特定组合仅包含一个结果,我们将确定哪些索引不NA
。这可以通过 !is.na(
轻松实现。如果我们有多于一行,我们使用 complete.cases(t
来获取仅包含数字的列 (即没有出现 NA
)。
此后,我们只需使用索引来获取结果的名称,瞧,我们就得到了想要的结果。
关于r - 估计 r 中面板数据的组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49200510/
我有一个 Bootstrap 面板,里面有另一个面板。两者在面板标题中都有一个字形图标。默认情况下,面板是折叠的。当面板折叠时,将设置 glyphicon-unchecked。当我点击这个字形图标时,
使用 Grafana 时,我们会在某些电视上使用它。有谁知道制作它的方法,如果有人添加面板,更改布局等,它不仅会自动刷新数据,还会自动刷新实际仪表板? 我们真的厌倦了不断要求具有远程访问权限的人登录并
基本上,我试图让屏幕底部有 3 个按钮,然后在屏幕中间有一个包含文字的标签。但是,我似乎无法同时在 GUI 中同时拥有按钮和标签。我是一名初学者,对布局了解不多(即使我已经阅读过它们),因此任何帮助/
作为学校的类(class)作业,我们必须提出一个自定义项目,我选择了一个原本打算在 ActionScript 上编写的游戏创意,但决定在 Java 上插入它。最初进展顺利,游戏代码正常运行, Spri
我正在尝试使用 Java Graphics 制作一个生命模拟游戏,但是当运行我的代码时,屏幕的左侧三分之一是灰色的。我希望整个屏幕是白色的,黑色方 block 代表生命方 block 。我对所有 ja
下面是动态驱动的一个不错的“下拉面板”。 http://www.dynamicdrive.com/dynamicindex17/dddropdownpanel.htm 如您所见,它是一个面板,在打开时
所以我有这个函数 onDisplayError ,如果请求失败,每次都会调用它。这意味着如果用户按下保存按钮并且 3 个请求失败,我当前会收到 3 个弹出消息。我的目标是此函数检查我的弹出窗口是否已打
我正在尝试为我的一个类(class)制作一款游戏,但在实现一些我想做的事情时遇到了麻烦。本质上,我希望在同一个图形用户界面窗口中包含多个不同的部分。 据我了解,我会使用 JPanel 来创建这些不同的
我目前正在测试这种类型的面板 jquery: http://codyhouse.co/gem/css-slide-in-panel/ 但是当我想检测从顶部滚动以显示一个 div 时:没办法:( 一种检
我可能正在搜索错误的问题,但我找不到这个问题的答案。 我有一个 AutoScroll 设置为 true 的面板。控件动态添加到面板。我需要在滚动条可见时触发一个事件,但我找不到这样的事件。 如有任何建
我有一堆 Bootstrap 面板,其中一些是相互关联的。有点像 Panel A -> Panel B -> Panel C 当用户单击面板 B 时,将显示面板 C,当单击面板 C 时,将显示面板 D
我正在尝试开发一种带有指标的下拉列表,并相应地针对应该出现在仪表板上的选定指标特定面板。反之亦然,如果未选择指标,则面板应隐藏。 我找到了链接 http://search-devops.com/m/k
我有一个窗口,窗口里面有面板。我动态地向面板添加组件。这些组件采用“hbox”布局,以便它们水平排列。单击按钮后,我将在“hbox”布局中再向面板添加一行类似组件。这里的问题是我想在第一行下面添加第二
我使用 jQuery Accordion 将表单拆分为多个面板,并使用 jQuery 验证来检查所需字段。只要验证字段位于打开的面板中,它就可以很好地显示已验证字段中的错误。 举个例子。假设我有三个
我正在尝试检查我的面板是打开还是关闭。 我尝试过这样的: $(document).on('open', '.ui-panel', function(){ console.log('open');
我有一个面板,我想将其 float 在所有窗口之上并出现在所有空间中。这可以通过 轻松完成 [self.panel setLevel:kCGUtilityWindowLevel]; [self.win
我想在格子中的单个面板上叠加多个组,并且想要独立的回归线。 获得多个面板相当容易,每个面板都有一条使用条件因子的回归线: xyplot( Petal.Width ~ Petal.Length |
Delphi 有滑动(动画)面板组件吗? 例如可以在 Raize Components 中找到它(带有“热点”或隐藏/显示按钮的左侧面板)。 我不需要一个可调整大小的面板,而是一个可以从左向右水平平滑
我有一个 Bootstrap Accordion : 我想禁用非事件面板中的所有输入控件。仅验证事件面板。 我有一个检测事件选项卡的功能: $(".panel").on("show.bs.collap
我正在尝试将组件放入不同尺寸的面板中。但是,我意识到 GridLayout 将大小相同的部分分开。如何实现如下图所示 enter image description here import java.
我是一名优秀的程序员,十分优秀!