Dcasting Error in R data frame

I have a data like this-
std_id Program_Code Rank
1 137520 22527 2
2 137520 24998 5
3 137520 29185 4
4 137520 7963 1
5 137524 13885 2
6 137524 4970 5
7 137524 16938 4
8 137524 14645 3
9 137525 14021 3
10 137525 18467 5
11 137525 16921 4
12 137525 12460 2
13 137525 7200 5
14 137526 30619 2
15 137526 30619 5
16 137526 5846 5
17 137530 32427 4

I want to do dcast-
ratingmat<- reshape2::dcast(ratings, std_id~ Program_Code, value.var="Rank", na.rm=F)
but it's showing error - Aggregation function missing: defaulting to length
*Error in .fun(.value[0], ...) : *

  • 2 arguments passed to 'length' which requires 1*

Kindly help!!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. It really make it easier to help.

I can point to help(dcast) and the example for understanding how the pieces fit

library(reshape2)
# from help(dcast)
names(airquality) <- tolower(names(airquality))
aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)
dcast(aqm, month ~ variable, mean, margins = c("month", "variable"))
#>   month    ozone  solar.r      wind     temp    (all)
#> 1     5 23.61538 181.2963 11.622581 65.54839 68.70696
#> 2     6 29.44444 190.1667 10.266667 79.10000 87.38384
#> 3     7 59.11538 216.4839  8.941935 83.90323 93.49748
#> 4     8 59.96154 171.8571  8.793548 83.96774 79.71207
#> 5     9 31.44828 167.4333 10.180000 76.90000 71.82689
#> 6 (all) 42.12931 185.9315  9.957516 77.88235 80.05722

Created on 2020-04-06 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.