想象一个人有5个桶(1 - 5),并且每个桶都分配一个(整数)值。 E.g。
> bucket = 1:5
> value = c(14, 12, 9, 20, 7)
> data.frame(bucket, value)
bucket value
1 1 14
2 2 12
3 3 9
4 4 20
5 5 7
然后,要求一个人增加总共3个桶的值(仅允许使用整数增加)。有几种方法可以在这些桶上分配总共3个(只允许使用整数增加)。
R中是否有一个函数可以在5个桶中提供总共3个的所有可能分布?
更具体地说,例如:
distr1 distr2 distr3 distr4 distr5 distr6 ....
1 3 2 2 2 2 1 ....
2 0 1 0 0 0 2 ....
3 0 0 1 0 0 0 ....
4 0 0 0 1 0 0 ....
5 0 0 0 0 1 0 ....
我调查了combn()
和expand.grid()
,但那些似乎不合适......
答案 0 :(得分:2)
这是一种可能性,expand.grid
,可能不是最优雅的:
n_buckets <- 5
increase <- 3
foo <- do.call(
expand.grid,
replicate(increase, seq_len(n_buckets), simplify = FALSE)
)
res <- apply(foo, 1, function(x) {
sapply(seq_len(n_buckets), function(y) sum(y == x))
})
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
# [1,] 3 2 2 2 2 2 1 1 1 1 2
# [2,] 0 1 0 0 0 1 2 1 1 1 0
# [3,] 0 0 1 0 0 0 0 1 0 0 1
# [4,] 0 0 0 1 0 0 0 0 1 0 0
# [5,] 0 0 0 0 1 0 0 0 0 1 0
# [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
# [1,] 1 1 1 1 2 1 1 1 1
# [2,] 1 0 0 0 0 1 0 0 0
# [3,] 1 2 1 1 0 0 1 0 0
# [4,] 0 0 1 0 1 1 1 2 1
# [5,] 0 0 0 1 0 0 0 0 1
#
# ...
注意:刚才注意到我的答案中存在冗余,因为我区分了将a
放入存储桶1
和b
存储桶中2
中的b
,以及1
中的a
和2
中的res[, !duplicated(res, MARGIN = 2)]
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
# [1,] 3 2 2 2 2 1 1 1 1 1 1
# [2,] 0 1 0 0 0 2 1 1 1 0 0
# [3,] 0 0 1 0 0 0 1 0 0 2 1
# [4,] 0 0 0 1 0 0 0 1 0 0 1
# [5,] 0 0 0 0 1 0 0 0 1 0 0
# [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
# [1,] 1 1 1 1 0 0 0 0 0 0
# [2,] 0 0 0 0 3 2 2 2 1 1
# [3,] 1 0 0 0 0 1 0 0 2 1
# [4,] 0 2 1 0 0 0 1 0 0 1
# [5,] 1 0 1 2 0 0 0 1 0 0
# [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31]
# [1,] 0 0 0 0 0 0 0 0 0 0
# [2,] 1 1 1 1 0 0 0 0 0 0
# [3,] 1 0 0 0 3 2 2 1 1 1
# [4,] 0 2 1 0 0 1 0 2 1 0
# [5,] 1 0 1 2 0 0 1 0 1 2
# [,32] [,33] [,34] [,35]
# [1,] 0 0 0 0
# [2,] 0 0 0 0
# [3,] 0 0 0 0
# [4,] 3 2 1 0
# [5,] 0 1 2 3
。你仍然可以获得所有可能性......
编辑:删除重复项:
java.lang.AbstractMethodError
org.hibernate.cache.ehcache.internal.nonstop.NonstopAwareEntityRegionAccessStrategy.get(Lorg/hibernate/engine/spi/SessionImplementor;Ljava/lang/Object;J)Ljava/lang/Object;
at org.hibernate.engine.internal.CacheHelper.fromSharedCache(CacheHelper.java:32) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.getFromSharedCache(DefaultLoadEventListener.java:651) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromSecondLevelCache(DefaultLoadEventListener.java:595) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:462) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:116) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:997) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:157) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:266) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at com.myapp.model.ProductEntity_$$_jvstdbe_0.getType(ProductEntity_$$_jvstdbe_0.java) ~[classes!/:2.1-SNAPSHOT]
at ma.glasnost.orika.generated.ProductTO_ProductEntity_ObjectFactory2345936633623123484334959983$4.create(ProductTO_ProductEntity_ObjectFactory2345936633623123484334959983$4.java) ~[orika-core-1.
5.0.jar!/:na]
at ma.glasnost.orika.impl.mapping.strategy.InstantiateAndUseCustomMapperStrategy.getInstance(InstantiateAndUseCustomMapperStrategy.java:55) ~[orika-core-1.5.0.jar!/:na]
at ma.glasnost.orika.impl.mapping.strategy.UseCustomMapperStrategy.map(UseCustomMapperStrategy.java:66) ~[orika-core-1.5.0.jar!/:na]
at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:254) ~[orika-core-1.5.0.jar!/:na]
at ma.glasnost.orika.impl.MapperFacadeImpl.mapElement(MapperFacadeImpl.java:802) ~[orika-core-1.5.0.jar!/:na]
at ma.glasnost.orika.impl.MapperFacadeImpl.mapAsCollection(MapperFacadeImpl.java:626) ~[orika-core-1.5.0.jar!/:na]
at ma.glasnost.orika.impl.MapperFacadeImpl.mapAsList(MapperFacadeImpl.java:424) ~[orika-core-1.5.0.jar!/:na]
at ma.glasnost.orika.impl.MapperFacadeImpl.mapAsList(MapperFacadeImpl.java:711) ~[orika-core-1.5.0.jar!/:na]
答案 1 :(得分:2)
切入:
首先,我们创建值0-3(invec
)的所有可能组合,然后我们使用rowSums
选择仅在所有存储桶中总计3的那些:
incvec <- 0:3
allDists <- expand.grid(b1=incvec,b2=incvec,b3=incvec,b4=incvec,b5=incvec)
finDists <- allDists[rowSums(allDists) == 3,]
> head(finDists)
b1 b2 b3 b4 b5
4 3 0 0 0 0
7 2 1 0 0 0
10 1 2 0 0 0
13 0 3 0 0 0
19 2 0 1 0 0
22 1 1 1 0 0
...
答案 2 :(得分:1)
使用grid.expand
确定可以调用哪些垃圾箱
I <- expand.grid(1:5,1:5,1:5)
然后为每行table
转换为data.frame
,将spread
转换为宽格式,然后使用data.frame
转换为map_df
library(purrr)
library(tidyr)
dist_df <- map_df(1:nrow(I), ~ as.data.frame(table(unlist(I[.x,]))) %>% spread(Var1,Freq)) %>%
replace(is.na(.), 0)
1 2 3 4 5
1 3 0 0 0 0
2 2 1 0 0 0
3 2 0 1 0 0
4 2 0 0 1 0
5 2 0 0 0 1
请注意,我的输出应该按行读取