R

dcast : Aggregation function missing: defaulting to length

R Aggregation function missing

If you are having this dcast warning Aggregation function missing: defaulting to length there might be a simple solution to this problem.

Here is my data frame.

df1 <- data.frame(
  date = as.POSIXct(c("2021-01-10", "2021-01-11", "2021-01-11", "2021-01-10", "2021-01-11", "2021-01-10", "2021-01-11")),
  agent = as.character(c("David", "David", "David", "Kate", "Kate", "Alma", "Alma")),
  value = as.numeric(c(701, 555, 555, 938, 698, 698, 690))
)
df1
#        date agent value
#1 2021-01-10 David   701
#2 2021-01-11 David   555
#3 2021-01-11 David   555
#4 2021-01-10  Kate   938
#5 2021-01-11  Kate   698
#6 2021-01-10  Alma   698
#7 2021-01-11  Alma   690

After reshaping data there is a dcast warning Aggregation function missing: defaulting to length. The results are not satisfying because I did that without any certain calculation.

reshaped_df1 <- reshape2::dcast(df, ...~agent, value.var = 'value', fun.aggregate = NULL)
#Aggregation function missing: defaulting to length

reshaped_df1

#        date Alma David Kate
#1 2021-01-10    1     1    1
#2 2021-01-11    1     2    1

Check if you have duplicates in your dataset

Check if you have duplicates. If the count of rows of the original dataset doesn’t match with the number of rows after you try to remove duplicates, then you have this problem.

Dcast cannot execute that kind of logic, where it is not clear what goes in which row.

Why do you have duplicates? Maybe something is missing that makes them unique?
If no other corrections are needed, remove them like this.

df2 <- df1 %>% dplyr::distinct()

If you have duplicates only in your dcast row values, then you should figure out what is redundant differently.

 

Dcast without warning “Aggregation function missing: defaulting to length”

 

As you can see, after removing duplicates dcast is running with no additional warnings.

Besides, here is how to execute dcast without calculation.

reshaped_df2 <- reshape2::dcast(df3, ...~agent, value.var = 'value', fun.aggregate = NULL)

df2

#        date Alma David Kate
#1 2021-01-10  698   701  938
#2 2021-01-11  690   555  698

 

Check out my favorite RStudio tips and tricks. For example, how to quickly view a data frame from R script.




0 comments on “dcast : Aggregation function missing: defaulting to length

Leave a Reply

Your email address will not be published. Required fields are marked *