【发布时间】:2020-04-08 08:11:31
【问题描述】:
在R 中,我有一个包含所有整数的2xn 数据矩阵。
第一列表示项目的大小。其中一些大小是由于合并造成的,因此第二列表示进入该大小的项目数(包括 1 个)(称为“索引”)。索引的总和表示原始数据中有多少项。
我现在需要创建一个新数据集,该数据集根据索引中的数字将任何合并的大小拆分回来,从而产生一个 2xn 向量(根据索引的总数具有新的长度 n)第二列全为 1。
我需要这种分裂以两种方式发生。
- “均匀地”,其中任何合并的大小都尽可能均匀地分配给索引的数量。例如,
6的大小和3的索引现在将是c(2,2,2)。 重要的是,所有数字都必须是整数,所以它应该是 c(1,2) 或 c(2,1) 之类的东西。不能是 c(1.5,1.5)。 - “异构”,其中大小数量倾斜以将
1分配给索引中的所有位置,除了一个包含提醒的位置。例如,大小为6且索引为3,现在将是c(1,1,4)或 1、1 和 4 的任意组合。
下面我提供了一些示例数据,这些数据举例说明了我拥有什么、我想要什么以及我尝试过什么。
#Example data that I have
Y.have<-cbind(c(19,1,1,1,1,4,3,1,1,8),c(3,1,1,1,1,2,1,1,1,3))
数据显示,第一行有 3 个项目的尺寸为 19,第二列有一个项目的尺寸为 1,以此类推。重要的是,在这些数据中最初有 15 个项目(即sum(Y.have[,2])),其中一些已合并,因此最终数据的长度需要为 15。
我希望数据看起来像:
####Homogenous separation - split values evenly as possible
#' The value of 19 in row 1 is now a vector of c(6,6,7) (or any combination thereof, i.e. c(6,7,6) is fine) since the position in the second column is a 3
#' Rows 2-5 are unchanged since they have a 1 in the second column
#' The value of 4 in row 6 is now a vecttor of c(2,2) since the position of the second column is a 2
#' Rows 7-9 are unchanged since they have a 1 in the second column
#' The value of 8 in row 10 is now a vector of c(3,3,2) (or any combination thereof) since the position in the second column is a 3
Y.want.hom<-cbind(c(c(6,6,7),1,1,1,1,c(2,2),3,1,1,c(3,3,2)),c(rep(1,times=sum(Y.have[,2]))))
####Heterogenous separation - split values with as many singles as possible,
#' The value of 19 in row 1 is now a vector of c(1,1,17) (or any combination thereof, i.e. c(1,17,1) is fine) since the position in the second column is a 3
#' Rows 2-5 are unchanged since they have a 1 in the second column
#' The value of 4 in row 6 is now a vecttor of c(1,3) since the position of the second column is a 2
#' Rows 7-9 are unchanged since they have a 1 in the second column
#' The value of 8 in row 10 is now a vector of c(1,1,6) (or any combination thereof) since the position in the second column is a 3
Y.want.het<-cbind(c(c(1,1,17),1,1,1,1,c(1,3),3,1,1,c(1,1,6)),c(rep(1,times=sum(Y.have[,2]))))
请注意,整数在最终数据中的位置无关紧要,因为它们都有一个索引案例。
我已尝试根据索引大小写拆分数据 (split)。这将根据唯一索引值的数量创建具有长度的列表。然后我遍历该列表中的位置并除以位置。
a<-split(Y.have[,1],Y.have[,2]) #Split into a list according to the index
b<-list() #initiate new list
for (i in 1:length(a)){
b[[i]]<-a[[i]]/i #get homogenous values
b[[i]]<-rep(b[i],times=i) #repeat the values based on the number of indicies
}
Y.test<-cbind(unlist(b),rep(1,times=length(unlist(c)))) #create new dataset
这是一种糟糕的方法。首先,它将产生小数。其次,列表中的位置不一定等于索引号(即,如果没有索引 2,则第二个位置将是下一个最低索引,但会除以 2)。
但是,它至少允许我按索引分离数据,对其进行操作,并将其重新组合成适当的长度。我现在需要中间部分的帮助 - 处理同质和异质重新分配的数据。我更喜欢base r,但任何方法都可以!提前谢谢!
【问题讨论】: