【发布时间】:2016-08-13 03:37:36
【问题描述】:
我的 R 代码如下。主要任务是计算重复的行数。
library(plyr)
data<-data.frame(1,2,3);
x <- read.table(text = "ID1 ID2 n m
13 156 12 15
94 187 14 16
66 297 41 48
29 89 42 49
78 79 51 79", header= TRUE)
distfunc <- function(data,ID1,ID2,n,m){
X1<-ID1; ################
X2<-ID2; ################
X3<-unlist(mapply(':', n, m));
data<-rbind(data,data.frame(X1,X2,X3));
return(data);
}
data<-distfunc(data,x$ID1, x$ID2,x$n, x$m)
data<-data[-1,]
plyr::count(data, names(data)); ## Calculates the row number of repetitions
我得到的错误信息:
Error in data.frame(X1, X2, X3) :
arguments imply differing number of rows: 5, 52
我尝试通过R Error: “In numerical expression has 19 elements: only the first used”修复它,但它失败了,结果是错误的。这个问题和那个问题不一样。
【问题讨论】:
-
您可以使用
debug(distfunc)探索它。阅读debug()的文档,了解如何处理调试器。 -
您正在使用
n和m的向量调用distfunc,但希望它们在该函数中作为数值工作。根据您要执行的操作,多次调用distfunc可能是正确的答案。
标签: r vectorization