0

I'm using dcast to swing some rows into columns

My data looks like this:

Place, Above.or.Below.Povertyline, Sex, Population Total
Haines, Above, Male, 55
Haines, Above, Female,7
Haines, Below, Male,0
Haines, Below, Female,7
Halibut, Above, Male,0   
Halibut, Above, Female,0

I used this command to dcast which gives me the following error:

df1 <- dcast(df, Place+ Above.or.Below.Povertyline ~ Sex, value.var = "Population Total")
Error: Aggregation function missing: defaulting to length

It gives me the following dataset which looks kinda right... except for the 8s. I think its duplicates? but I'm not sure if I can format the function better.

Community, Above.or.Below.Povertyline, Female, Male
Haines, Above, 8, 8
Haines, Below, 8, 8
Halibut, Above, 8, 8
Halibut, Below, 8, 8
  • It's a warning (not an error) to let you know that at least one cell in the output table has more than one value. If you don't supply an aggregation function to `dcast` using the `fun.aggregate` argument, then `dcast` defaults to giving a count of the number of values in each cell (by using the `length` function as the aggregation function). – eipi10 Jun 20 '17 at 21:49
  • @eipi10 that makes more sense. Would I have to add that using a comma after the value.var = "Population Total"? – Emma Jun 20 '17 at 21:51
  • For example, try each of the following with the built-in `mtcars` data frame: `dcast(mtcars, vs ~ carb, value.var="mpg")`, `dcast(mtcars, vs ~ carb, value.var="mpg", fun.aggregate=mean)`, `dcast(mtcars, vs ~ carb, value.var="mpg", fun.aggregate=length)` – eipi10 Jun 20 '17 at 21:51
  • @eipi10 thank you I will experiment with that. It ended up working except for the Female column... the above and below rows for the female column are exactly the same. Weird. – Emma Jun 20 '17 at 21:56
  • One other thing to note: If all of the cells have zero or one value in the cast data frame, then `dcast` will by default return the actual values, rather than a count of values. For example, try `dat=expand.grid(g1=1:10, g2=11:20); dat$value = rnorm(100); dcast(dat, g1 ~ g2, value.var="value")` – eipi10 Jun 20 '17 at 21:58
  • Related: [*dcast error: ‘Aggregation function missing: defaulting to length’*](https://stackoverflow.com/q/33051386/2204410) – Jaap Aug 31 '18 at 05:35

0 Answers0