Reputation: 367
I would like to apply the following function to a dataframe, of which a sample is found below. I had the function working without using dplyr functions, but wanted to apply group_by to group by year. Since that was not possible, I used the dplyr commands. Now, however, I continue to get all sorts of error messages, depending on how I try to solve the problem. The last one was:
Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "function".
Since I am not making headway no matter what I do, I would greatly appreciate your support. The problems seem to be associated with the group_by function. I am new at R and this is the first function I am writing, so please be gentle. Thanks a lot.
depDistOverParties=function(DF, year, percVote, deps){
group_by(DF, year) %>%
mutate(DF, depsIdeal = (percVote / 100) * deps,
depsWhole = floor(depsIdeal),
depsPart = depsIdeal - depsWhole)
arrange(DF, desc(depsPart))
mutate(DF, depsLeft = round(sum(depsPart)),
n = nrow(percVote),
depsZero = n - depsLeft,
addDeps = c(rep(1, depsLeft), rep(0, depsZero)),
totalLevelDeps = depsWhole + addDeps)
}
Here is the execution of the function:
df2=depDistOverParties(df, df$year, df$percVotesYear, df$totalSeats350)
Here is the sample data:
df=structure(list(year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
2008L, 2008L, 2008L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L), party = structure(c(2L,
4L, 5L, 10L, 11L, 13L, 14L, 15L, 16L, 1L, 2L, 4L, 5L, 6L, 9L,
10L, 11L, 13L, 14L, 15L, 16L, 3L, 5L, 10L, 11L, 12L, 13L, 8L,
14L, 15L, 3L, 5L, 7L, 10L, 12L, 13L, 8L, 14L, 15L), .Label = c("AMAIUR",
"BNG", "C", "CC", "CIU", "COMPROMIS", "EHBILDU", "PODEMOS", "EQUO",
"ERC", "IU", "PACMA", "PNV", "PP", "PSOE", "UPYD"), class = "factor"),
totalVotes = c(212543L, 174629L, 779425L, 298139L, 969946L,
306128L, 10278010L, 11289335L, 306079L, 334498L, 184037L,
143881L, 1015691L, 125306L, 216748L, 256985L, 1686040L, 324317L,
10866566L, 7003511L, 1143225L, 3556267L, 567253L, 601782L,
939204L, 221602L, 302316L, 5247050L, 7310128L, 5590209L,
3141570L, 483488L, 184713L, 632234L, 286702L, 287014L, 5087538L,
7941236L, 5443846L), totalSeats = c(2L, 2L, 10L, 3L, 2L,
6L, 154L, 169L, 1L, 7L, 2L, 2L, 16L, 1L, 0L, 3L, 11L, 5L,
186L, 110L, 5L, 41L, 8L, 9L, 2L, 0L, 6L, 70L, 123L, 90L,
32L, 8L, 2L, 9L, 0L, 5L, 71L, 137L, 85L), totalSeats350 = c(350,
350, 350, 350, 350, 350, 350, 350, 350, 350, 350, 350, 350,
350, 350, 350, 350, 350, 350, 350, 350, 350, 350, 350, 350,
350, 350, 350, 350, 350, 350, 350, 350, 350, 350, 350, 350,
350, 350), totalVotesYear = c(24614234L, 24614234L, 24614234L,
24614234L, 24614234L, 24614234L, 24614234L, 24614234L, 24614234L,
23300805L, 23300805L, 23300805L, 23300805L, 23300805L, 23300805L,
23300805L, 23300805L, 23300805L, 23300805L, 23300805L, 23300805L,
24335811L, 24335811L, 24335811L, 24335811L, 24335811L, 24335811L,
24335811L, 24335811L, 24335811L, 23488341L, 23488341L, 23488341L,
23488341L, 23488341L, 23488341L, 23488341L, 23488341L, 23488341L
), percVotesYear = c(0.863496300555199, 0.709463475483332,
3.1665620794862, 1.2112463056945, 3.94058982294554, 1.24370313534843,
41.7563674742021, 45.8650673427416, 1.24350406354307, 1.43556413608886,
0.789831080943341, 0.617493687449854, 4.3590382392368, 0.537775411622045,
0.930216788647431, 1.10290180961559, 7.23597317775073, 1.39187036671051,
46.6360110734372, 30.0569486762367, 4.90637555226096, 14.6133079353715,
2.33093937161166, 2.47282492455255, 3.8593494993859, 0.910600431602629,
1.24226803043465, 21.5610237932896, 30.0385633336814, 22.9711226800701,
13.3750186954455, 2.0584169822807, 0.78640292219872, 2.69169287009244,
1.22061409105053, 1.2219424096406, 21.6598439200112, 33.8092673296935,
23.1768007795868), totalSeatsYear = c(349L, 349L, 349L, 349L,
349L, 349L, 349L, 349L, 349L, 348L, 348L, 348L, 348L, 348L,
348L, 348L, 348L, 348L, 348L, 348L, 348L, 349L, 349L, 349L,
349L, 349L, 349L, 349L, 349L, 349L, 349L, 349L, 349L, 349L,
349L, 349L, 349L, 349L, 349L), percSeatsYear = c(0.571428571428571,
0.571428571428571, 2.85714285714286, 0.857142857142857, 0.571428571428571,
1.71428571428571, 44, 48.2857142857143, 0.285714285714286,
2, 0.571428571428571, 0.571428571428571, 4.57142857142857,
0.285714285714286, 0, 0.857142857142857, 3.14285714285714,
1.42857142857143, 53.1428571428571, 31.4285714285714, 1.42857142857143,
11.7142857142857, 2.28571428571429, 2.57142857142857, 0.571428571428571,
0, 1.71428571428571, 20, 35.1428571428571, 25.7142857142857,
9.14285714285714, 2.28571428571429, 0.571428571428571, 2.57142857142857,
0, 1.42857142857143, 20.2857142857143, 39.1428571428571,
24.2857142857143), disprop = c(-0.292067729126628, -0.138034904054761,
-0.309419222343346, -0.354103448551645, -3.36916125151697,
0.470582578937282, 2.24363252579788, 2.42064694297269, -0.957789777828785,
0.56443586391114, -0.218402509514769, -0.0460651160212828,
0.212390332191772, -0.252061125907759, -0.930216788647431,
-0.245758952472733, -4.09311603489358, 0.0367010618609223,
6.50684606941999, 1.37162275233471, -3.47780412368953, -2.89902222108574,
-0.0452250858973695, 0.0986036468760259, -3.28792092795733,
-0.910600431602629, 0.47201768385106, -1.56102379328965,
5.10429380917576, 2.74316303421559, -4.2321615525884, 0.227297303433587,
-0.214974350770148, -0.120264298663872, -1.22061409105053,
0.206629018930833, -1.37412963429692, 5.33358981316365, 1.10891350612751
)), class = "data.frame", row.names = c(NA, -39L))
Upvotes: 1
Views: 177
Reputation: 13135
depDistOverParties=function(DF, year, percVote, deps){
year <- enquo(year)
percVote <- enquo(percVote)
deps <- enquo(deps)
#browser()
DF %>% group_by(!! year) %>%
mutate(depsIdeal = (!! percVote / 100) * !! deps,
depsWhole = floor(depsIdeal),
depsPart = depsIdeal - depsWhole) %>%
arrange(desc(depsPart))
#This part throws an error but I don't know why
#mutate(depsLeft = round(sum(depsPart)),
# n = nrow(!! percVote),
# depsZero = n - depsLeft,
# addDeps = c(rep(1, depsLeft), rep(0, depsZero)),
# totalLevelDeps = depsWhole + addDeps)
}
depDistOverParties(df, year, percVotesYear, totalSeats350)
To programming using dplyr
check here
Upvotes: 1