我想知道为什么我的函数返回一个错误,当我知道它们没有时,我的参数有不同的长度。该函数应创建表,使用for循环将向量与属于同一data.frame的其他几个向量进行比较。下面有一些示例数据和功能...感谢您提前提供任何帮助。
mbr.type <- c('New_Mbr', 'Zero_Mbr','newSingle', 'newJoints', 'singleApp')
structure(list(singleApp = c(1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 0, 0, 1, 0), cgrp = structure(c(3L, 3L, 1L, 4L,
2L, 3L, 3L, 1L, 4L, 1L, 2L, 1L, 4L, 3L, 1L, 2L, 2L, 3L, 3L, 3L
), .Label = c("A", "B", "C", "D"), class = "factor"), B1_CreditScore = c(651,
636, 793, 453, 672, 656, 622, 796, 0, 729, 714, 779, 560, 627,
791, 674, 693, 640, 646, 640), Join_Days = c(4953, 0, 13485,
3749, 862, 4394, 689, 2561, 1766, 1507, 6314, 3093, 3942, 6223,
210, 7138, 3002, 3996, 2811, 0), Collection = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CREDIT_LIMIT = c(500,
5000, 15000, 0, 0, 0, 0, 15000, 500, 0, 0, 0, 0, 0, 0, 0, 500,
0, 0, 0), HighestJointScore = c(0, 0, 0, 832, 0, 0, 0, 0, 0,
0, 0, 0, 669, 0, 0, 0, 542, 662, 0, 729), New_Mbr = c(0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), product = c("cc",
"cc", "cc", "pl", "pl", "pl", "pl", "cc", "cc", "pl", "pl", "pl",
"pl", "pl", "pl", "pl", "pl", "pl", "pl", "pl"), Zero_Mbr = c(0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), newSingle = c(0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), newJoints = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), joint = c(0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1), collSSNMbr = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), id = c("151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df", "151aff42b0f2d2654d39df270ab411df",
"151aff42b0f2d2654d39df270ab411df")), .Names = c("singleApp",
"cgrp", "B1_CreditScore", "Join_Days", "Collection", "CREDIT_LIMIT",
"HighestJointScore", "New_Mbr", "product", "Zero_Mbr", "newSingle",
"newJoints", "joint", "collSSNMbr", "id"), .internal.selfref = <pointer: 0x0000000000120788>, row.names = c(15794L,
13346L, 7703L, 1024L, 10068L, 9268L, 9262L, 11227L, 16059L, 11861L,
13763L, 1307L, 928L, 9111L, 5086L, 4715L, 6832L, 6104L, 7193L,
1292L), class = c("data.table", "data.frame"))
tableList <- function(x, y, data, ...){
if(!is.character(y)){
stop('y must be a character vector')
}
if(!all(mbr.type %in% colnames(data))){
stop('all y must be included in data')
}
if(!is.character(x)){
stop('x must be a character vector')
}
table.list <- list()
my.call <- match.call(expand.dots = TRUE)
my.call[[1]] <- as.name('table')
#data <- data
my.call[['x']] <- data[[x]]
for(i in 1:length(y)){
my.call[['y']] <- data[[y[i]]]
table.list[[i]] <- eval(my.call)
}
}
tableList(x = 'Collection', y = mbr.type, data = sampleSO)
表中的错误(x = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,: 所有参数必须具有相同的长度b
答案 0 :(得分:0)
你可能已经跑了这个:
library(plyr)
我假设allProducts看起来像这样:
allProducts <- cbind("Prod 1", "Prod 2", "Prod 3", "Prod 4", "Prod 5")
colnames(allProducts) <- c('New_Mbr', 'Zero_Mbr', 'newSingle', 'newJoints', 'singleApp')
我认为问题在于它试图将expand.dots
参数传递给表函数。由于这不是有效的参数名称,因此它会尝试在表格中加入TRUE
,其长度不等于您的x
或y
(20)。
也许我误解了你想要做的事情......这会解决你的问题吗?...
tableList <- function(myx, myy, data, ...) {
if (!is.character(myy)) {
stop('y must be a character vector')
}
if (!all(mbr.type %in% colnames(allProducts))) {
stop('all y must be included in data')
}
if (!is.character(myx)) {
stop('x must be a character vector')
}
table.list <- list()
x <- data[[myx]]
for (i in 1:length(myy)) {
y <- data[[myy[i]]]
table.list[[i]] <- table(x, y)
}
table.list
}
tableList(myx = 'Collection', myy = mbr.type, data = sampleSO)