2017-02-26 22 views
0

我想知道爲什麼我的函數返回一個錯誤,我的參數有不同的長度,當我知道他們沒有。該函數應該創建表格,使用for循環將矢量與屬於相同data.frame的其他幾個矢量進行比較。一些示例數據和功能如下...感謝您提前提供任何幫助。使用R函數時不同的長度

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

什麼是'allProducts'? –

+0

'table(1:4,1:3)'拋出相同的錯誤,出於同樣的原因:'nrow(data)!= length(mbr.type)'。 – r2evans

+0

@ r2evans我不確定這適用於此。我的函數將整個'Collection'向量傳遞給參數x,並將字符串mbr.type的每個元素的整個向量傳遞給參數y。它循環遍歷每個元素與「集合」具有相等的長度。 – user3067851

回答

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,其長度不等於您的xy(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) 
+0

我的錯誤。所有產品應該是'數據'。只需進行錯誤檢查,以確保數據中存在mbr.type中列出的列名稱。 – user3067851