[英]R - Replace a double loop by a function from the apply family
I have these loops : 我有这些循环:
xall = data.frame()
for (k in 1:nrow(VectClasses))
{
for (i in 1:nrow(VectIndVar))
{
xall[i,k] = sum(VectClasses[k,] == VectIndVar[i,])
}
}
The data: 数据:
VectClasses = Data Frame containing the characteristics of each classes VectClasses =包含每个类的特征的数据框
VectIndVar = Data Frame containing each record of the data base VectIndVar =包含数据库的每条记录的数据框
The two for loops work and give an output I can work with, however, it takes too long, hence my need for the apply family 两个for循环工作并提供我可以使用的输出,但是,它需要太长时间,因此我需要apply系列
The output I am looking for is as this: 我正在寻找的输出是这样的:
V1 V2 V3 V4
1 3 3 2 2
2 2 2 1 1
3 3 4 3 3
4 3 4 3 3
5 4 4 3 3
6 3 2 3 3
I tried using : 我试过用:
xball = data.frame()
xball = sapply(xball, function (i,k){
sum(VectClasses[k,] == VectIndVar[i,])})
xcall = data.frame()
xcall = lapply(xcall, function (i, k){sum(VectClasses[k,] == VectIndVar[i,]} )
but neither seems to be filling the dataframe 但似乎都没有填补数据框架
reproductible data (shortened): 可再现数据(缩短):
VectIndVar <- data.frame(a=sample(letters[1:5], 100, rep=T), b=floor(runif(100)*25),
c = sample(c(1:5), 100, rep=T),
d=sample(c(1:2), 100, rep=T))
and : 并且:
> K1 = 4
VectClasses= VectIndVar [sample(1:nrow(VectIndVar ), K1, replace=FALSE), ]
Can you help me? 你能帮助我吗?
I would use outer
instead of *apply
: 我会使用outer
而不是*apply
:
res <- outer(
1:nrow(VectIndVar),
1:nrow(VectClasses),
Vectorize(function(i,k) sum(VectIndVar[i,-1]==VectClasses[k,-1]))
)
(Thanks to this Q&A for clarifying that Vectorize
is needed.) (感谢此问答以澄清需要Vectorize
。)
This gives 这给了
> head(res) # with set.seed(1) before creating the data
[,1] [,2] [,3] [,4]
[1,] 1 1 2 1
[2,] 0 0 1 0
[3,] 0 0 0 0
[4,] 0 0 1 0
[5,] 1 0 0 1
[6,] 1 1 1 1
As for speed, I would suggest using matrices instead of data.frames: 至于速度,我建议使用矩阵而不是data.frames:
cmat <- as.matrix(VectClasses[-1]); rownames(cmat)<-VectClasses$a
imat <- as.matrix(VectIndVar[-1]); rownames(imat)<-VectIndVar$a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.