Using aggregate in a function

Pedro Fontanarrosa

So I am using the function aggregate for the marginalization of conditional probability tables, which works perfectly when used in the console line.

ab                               
  x probs y  
1 F  0.28 T  
2 F  0.42 F  
3 T  0.24 T  
4 T  0.06 F

and use the following:

a  = aggregate(probs ~ y , ab, sum)

produces:

a  
  y probs  
1 F  0.48  
2 T  0.52

which is exacly what I want the following custom function to do:

marginalizeFactor = function(A, margVar)
{
  a = aggregate(probs ~ y + z, A, sum)

  return(a)
}

margVar being a string with a character of the variable to be marginalized (for example magVar = "x").
My problem is, I don't know how to make this function work with any type of string character which is called (the function has to work not only with tables with variables "x" or "y", it could also be "fuel" etc).
And the function aggregate seems to not take strings or lists as a valid argument for grouping variable. HELP???? how to make the following:

marginalizeFactor = function(A, margVar)
    {
      a = aggregate(probs ~ magVar, A, sum)

      return(a)
    }

Being that margVar is a string type (I mean, magVar cannot be margVar = x because the data type error pops up, so, margVar = "x")

G5W

In your second version, you have a typo. magVar -> margVar.

Yes, you cannot just use a string in the formula. you need something like this.

marginalizeFactor = function(A, margVar) {
    a = aggregate(formula(paste("probs ~", margVar)), A, sum)
    return(a)
}

marginalizeFactor(ab, 'y')
      y probs
1 FALSE  0.48
2  TRUE  0.52

Actually, the function might be a little better as

marginalizeFactor = function(A, margVar) {
    aggregate(formula(paste("probs ~", margVar)), A, sum)
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Using the "aggregate" function for drawing line plots

分類Dev

MySQL Why automatically sum when using aggregate function

分類Dev

SOLVED Sequelize get data of another column while using Aggregate Function

分類Dev

create new column in a dataframe using custom aggregate function in pandas

分類Dev

Delphi : incorrect parameter message error with AdoQuery using SQL aggregate function

分類Dev

Array intersection as aggregate function for group by

分類Dev

Problems with SQL sum aggregate function

分類Dev

MongoDB Aggregate function to C#

分類Dev

Groupby and aggregate using lambda functions

分類Dev

Group by and Aggregate dataset using Python

分類Dev

select aggregate function and all other columns

分類Dev

Aggregate function in R zoo returns error

分類Dev

Creating a Custom Aggregate Function for Access Queries

分類Dev

pandas aggregate function in groupby - default option?

分類Dev

Keep Column which is not in aggregate function in group by statement

分類Dev

python pandas groupby aggregate with min function

分類Dev

ERROR: aggregate function calls cannot be nested - POSTGRESQL

分類Dev

Using EventScheduler does not trigger event handlers in Aggregate

分類Dev

How to calculate the running total using aggregate

分類Dev

Merge xml files using Aggregate mediator

分類Dev

How to sort date using aggregate from mongodb

分類Dev

SQL Update using join and conditional aggregate

分類Dev

Mongodb aggregate query using group-by

分類Dev

Should I avoid using aggregate roots?

分類Dev

SUM, Count, and convert - "cannot perform an aggregate function on an expression containing an aggregate or a subquery"

分類Dev

SQL Pivot with dynamic generated columns, aggregate function and columns without aggregation

分類Dev

Aggregate function after HAVING clause returning empty set

分類Dev

Aggregate function enhancements to include multiple fields without _id field

分類Dev

Linq equivalent of aggregate function on multiple tables in one database trip

Related 関連記事

  1. 1

    Using the "aggregate" function for drawing line plots

  2. 2

    MySQL Why automatically sum when using aggregate function

  3. 3

    SOLVED Sequelize get data of another column while using Aggregate Function

  4. 4

    create new column in a dataframe using custom aggregate function in pandas

  5. 5

    Delphi : incorrect parameter message error with AdoQuery using SQL aggregate function

  6. 6

    Array intersection as aggregate function for group by

  7. 7

    Problems with SQL sum aggregate function

  8. 8

    MongoDB Aggregate function to C#

  9. 9

    Groupby and aggregate using lambda functions

  10. 10

    Group by and Aggregate dataset using Python

  11. 11

    select aggregate function and all other columns

  12. 12

    Aggregate function in R zoo returns error

  13. 13

    Creating a Custom Aggregate Function for Access Queries

  14. 14

    pandas aggregate function in groupby - default option?

  15. 15

    Keep Column which is not in aggregate function in group by statement

  16. 16

    python pandas groupby aggregate with min function

  17. 17

    ERROR: aggregate function calls cannot be nested - POSTGRESQL

  18. 18

    Using EventScheduler does not trigger event handlers in Aggregate

  19. 19

    How to calculate the running total using aggregate

  20. 20

    Merge xml files using Aggregate mediator

  21. 21

    How to sort date using aggregate from mongodb

  22. 22

    SQL Update using join and conditional aggregate

  23. 23

    Mongodb aggregate query using group-by

  24. 24

    Should I avoid using aggregate roots?

  25. 25

    SUM, Count, and convert - "cannot perform an aggregate function on an expression containing an aggregate or a subquery"

  26. 26

    SQL Pivot with dynamic generated columns, aggregate function and columns without aggregation

  27. 27

    Aggregate function after HAVING clause returning empty set

  28. 28

    Aggregate function enhancements to include multiple fields without _id field

  29. 29

    Linq equivalent of aggregate function on multiple tables in one database trip

ホットタグ

アーカイブ