Search code examples
rdata.tablereshape2dcast

dcast for numeric and character columns in R - returning length by default


I have a data which looks like this :-

data_source   zip        date        calories      user            price
 compA        45768      18274        3500          abc             912.27
 compB        33098      18274        3500          groups          981.28
 compA        39104      18274        2500          ands            659.75

I would like to have wide format of data using dcast; Earlier it use to work, but now it does not.

data.table::dcast(zip + date + calories ~ data_source, value.var=c("user","price"), data=data)

As you can see the column in value.var has character and numeric value both, and so I'm confused what to use in fun.aggregate. So the data converted is defaulting to length which is what I do not want. I just want the values as it is but in wide format. Thanks for your help.


Solution

  • We can specify length in fun.aggregate if the length is needed

    library(data.table)
    dcast(setDT(data), zip + date + calories ~ data_source, 
           value.var=c("user","price"), length)
    

    Based on the data showed, there are no duplicates, so it would work

    dcast(setDT(data), zip + date + calories ~ data_source, value.var=c("user","price"))
    

    If there are duplicates, make a correction to have unique combinations by adding rowid for the grouping variable

    dcast(setDT(data), rowid(zip, date, calories) + zip + date + calories 
              ~ data_source, value.var=c("user","price"))