【问题标题】:Differing lengths when using R function使用 R 函数时的不同长度
【发布时间】:2017-02-26 02:55:03
【问题描述】:

我想知道为什么我的函数返回一个错误,即我的参数长度不同,而我知道它们没有。该函数应该使用 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

【问题讨论】:

  • 什么是allProducts
  • table(1:4, 1:3) 抛出相同的错误,原因相同:nrow(data) != length(mbr.type)
  • @r2evans 我不确定这是否适用于此。我的函数将整个“集合”向量传递给参数 x,并将字符串 mbr.type 的每个元素的整个向量传递给参数 y。它遍历每个长度与“集合”相等的元素。
  • 我认为这正是重点:您的两个向量大小不相等,这是table() 的先决条件(除非我误解了事物)。整个Collection向量长度为​​20,mbr.type长度为5。
  • 是的,你误会了。 mbr.type 向量包含 5 个元素,每个元素一列数据。循环应该采用 mbr.type 的第一个元素 - 'New_Mbr' 并将长度为 20 的列与长度为 20 的列进行比较。在循环之后,5 个表应该包含在一个列表中将 mbr.type 的每个元素与“Collection”进行比较。

标签: r dataframe


【解决方案1】:

你可能已经运行了这个:

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)

【讨论】:

  • 我的错误。 allProducts 应该是“数据”。只需进行错误检查以确保 mbr.type 中列出的列名存在于数据中。
猜你喜欢
  • 2014-01-15
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多