【问题标题】:Aggregate character/string variables聚合字符/字符串变量
【发布时间】:2017-09-14 10:59:35
【问题描述】:

我有一个数据框,其中包含人员参考列表和交易,每个人参考可以有多个交易。我想聚合,使其显示如下数据:

Record Person Ref       Trade Code
1        512       elec, plumbing, gas
2        654            gas, plumbing
3        685              elec

等等。

我现在的代码是:

TEST<-aggregate(`Trade Code`~`Person Reference`, df, function(test)
      paste(names(table(test)[rank(-1*table(test),ties.method="min")==1],collapse=NULL))

这给出了错误:

> Error: unexpected symbol in: "TEST<-aggregate('Trade Code'~'Person
> Reference', age_arr, function(test)
> paste(names(table(test)[rank(-1*table(test),ties.method="min")==1],collapse=NULL))
> View"

谁能看出我哪里出了问题?

【问题讨论】:

    标签: r function aggregate paste collapse


    【解决方案1】:

    您可以使用 dplyr 和 toString 做到这一点:

    df <- read.table(text="Record Person_Ref Trade_Code
    1        512       elec
    1        512       plumbing
    1        512       gas
    2        654       gas
    2        654       plumbing
    3        685       elec",header=TRUE,stringsAsFactors=FALSE)
    
    df%>%
    group_by(Record,Person_Ref) %>%
    summarise(catY = toString(Trade_Code))
    
      Record Person_Ref                catY
       <int>      <int>               <chr>
    1      1        512 elec, plumbing, gas
    2      2        654       gas, plumbing
    3      3        685                elec   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-17
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多