变量'paste'的聚合错误'无效类型(闭包)

时间:2014-01-08 08:55:04

标签: r aggregate

对于this question @ SimonO101建议aggregate解决方案的评论,如下所示:

#dataframe
df <- data.frame(id=c(101,102,102,103,104,104,104),
                 calmonth=c('01','01','01','01','01','01','02'),
                 product=c('apple','apple','htc','htc','apple','htc','nokia'),
                 bb=sample(1:20,7))

#aggregate - SimonO101's solution:
aggregate(product~id+calmonth, data=df, FUN=paste, collapse=" & ")

#Error
  

model.frame.default中的错误(公式=〜产品+粘贴,数据=列表(id = c(101,:   变量'paste'的无效类型(闭包)

无法找出导致此错误的原因?

版本:

Windows 7 Enterprise SP1, 64bit OS, "R version 3.0.1 (2013-05-16)", RStudio

更新:打开新的RStudio会话,没有错误,仍想查明原因。

更新: dput:

dput(df123)
structure(list(id = c(101, 102, 102, 103, 104, 104, 104), calmonth = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L), .Label = c("01", "02"), class = "factor"), 
    product = structure(c(1L, 1L, 2L, 2L, 1L, 2L, 3L), .Label = c("apple", 
    "htc", "nokia"), class = "factor"), bb = c(19L, 17L, 5L, 
    15L, 16L, 4L, 9L)), .Names = c("id", "calmonth", "product", 
"bb"), row.names = c(NA, -7L), class = "data.frame")

更新冲突

conflicts()
 [1] "dbCallProc"        "dbClearResult"     "dbColumnInfo"      "dbCommit"         
 [5] "dbConnect"         "dbDataType"        "dbDisconnect"      "dbExistsTable"    
 [9] "dbGetException"    "dbGetInfo"         "dbGetQuery"        "dbGetRowCount"    
[13] "dbGetRowsAffected" "dbGetStatement"    "dbHasCompleted"    "dbListConnections"
[17] "dbListFields"      "dbListResults"     "dbListTables"      "dbReadTable"      
[21] "dbRemoveTable"     "dbRollback"        "dbSendQuery"       "dbUnloadDriver"   
[25] "dbWriteTable"      "fetch"             "isSQLKeyword"      "make.db.names"    
[29] "SQLKeywords"       "summary"           "fillin"            "recode"           
[33] "untangle.specials" "%nin%"             "codebook"          "coerce"           
[37] "initialize"        "recode"            "rename"            "show"             
[41] "summary"           "dotplot"           "contr.sum"         "contr.treatment"  
[45] "contrasts"         "contrasts<-"       "Arith"             "coerce"           
[49] "Compare"           "initialize"        "Math"              "Math2"            
[53] "show"              "Summary"           "%in%"              "as.array"         
[57] "as.factor"         "as.ordered"        "as.vector"         "body<-"           
[61] "format"            "format.pval"       "kronecker"         "labels"           
[65] "print"             "round.POSIXt"      "row.names"         "sample"           
[69] "subset"            "summary"           "trunc.POSIXt"      "unique"           
[73] "units"             "within"

1 个答案:

答案 0 :(得分:3)

对我有用......

> df <- data.frame(id=c(101,102,102,103,104,104,104),
+                  calmonth=c('01','01','01','01','01','01','02'),
+                  product=c('apple','apple','htc','htc','apple','htc','nokia'),
+                  bb=sample(1:20,7))
> 
> #aggregate - SimonO101's solution:
> aggregate(product~id+calmonth, data=df, FUN=paste, collapse=" & ")
   id calmonth     product
1 101       01       apple
2 102       01 apple & htc
3 103       01         htc
4 104       01 apple & htc
5 104       02       nokia
> 

尝试conflicts(),看看你是否正在掩饰......