Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/Questions/Stabilizing the error with the best answers: aggregation function missing: defaulting to length
Next
Answered
Emma Wilson
  • 11
Emma Wilson
Asked: May 18, 20222022-05-18T20:44:16+00:00 2022-05-18T20:44:16+00:00In: r

Stabilizing the error with the best answers: aggregation function missing: defaulting to length

  • 11

I get the error: aggregation function missing: defaulting to length when I try to run the program below:

Id Task Type Freq 
 3 1 A 2
 3 1 B 3
 3 2 A 3
 3 2 B 0
 4 1 A 3
 4 1 B 3
 4 2 A 1
 4 2 B 3
Id A B … Z 
 3 5 3 
 4 4 6
df_wide <- dcast(df, Id + Task ~ Type, value.var="Freq")

The error appears the system notifies as follows:

Aggregation function missing: defaulting to length

I tried to solve it with another sample. I got the reference in the community forum, but it still returned an invalid result. If someone knows the solution, please give me the support. Thanks!

aggregation function missing
  • 2 2 Answers
  • 140 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-29T16:45:09+00:00Added an answer on June 29, 2022 at 4:45 pm

    The cause:

    You have got this error because the dcast function which is in the reshape2 package is being used to change a data frame from a length to width format. However, there are more than one value which can be set in the single cell of the width data frame. When many values from the value.var column that correspond to the same value(s) or combination of values appearing on the LHS of the dcast formula (for example, “Id”) are crammed into one cell by the combination of variables in the RHS of the calculation, fun.aggregate is necessary (for example, “Type”).

    Solution:

    The length() default value in dcast is instructive because it can point to the possibility of coupling in the data and identify length > 1 cases that could need special attention.
    Using list() as fun.aggregate would be more instructive because it displays which value.var values are related for each instance as the following:

    dcast(dt, Id ~ Type, fun.aggregate = list, value.var = 'Freq')
    
    Id A B
    1: 3 2,3 3,0
    2: 4 3,1 3,3

    Table cells typically have a length of 1. Consequently, the defaulting issue in dcast can be resolved by changing the formula or by putting in place a length-one summarization (aggregation): operators, custom or ready-made functions that deliver a length-one result in each case and are appropriate for the task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Juliette Schneider
    2022-05-25T21:08:47+00:00Added an answer on May 25, 2022 at 9:08 pm

    This warning is caused by fun.aggregate (see ?dcast).

    aggregation function needed if variables do not identify a single
    observation for each output cell. Defaults to length (with a message)
    if needed but not specified

    An aggregation function is required when more than one value is available for a single spot in the large dataframe.



    Based on your data,

    Use dcast(df, Id + Task ~ Type, value.var="Freq") to get:

     Id Task A B
    1 3 1 2 3
    2 3 2 3 0
    3 4 1 3 3
    4 4 2 1 3

    This is because Id, Task and Type only have one value in Freq. This warning message is also displayed when you use dcast(df, Id ~ Type, value.var="Freq")

    Aggregation function missing: defaulting to length
     Id A B
    1 3 2 2
    2 4 2 2

    Let’s now look at the top portion of your data.

    Id Task Type Freq 
    3 1 A 2
    3 1 B 3
    3 2 A 3
    3 2 B 0

    This is why it is. There are two codes in Freq for each combination of Id & Type (for Id 3 2 & 3 for type A & Type 0), while only one can be put in this spot in wide dataframe for each value of type. dcast will combine these values into one value. length is the default aggregation function. However, you can specify other functions such as sum, mean or sd by using fun.aggregate.

    fun.aggregate = sum, for example, gives you:

     Id A B
    1 3 5 3
    2 4 4 6

    There is no warning, dcast is being told exactly what to do when more than one value is present: return the total of all values.

    • 13
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.