psysky
psysky

Reputation: 3195

Error: Problem with `mutate ()` input `..1`. when aggregate data in R

Here my data

mydat=structure(list(id = c(15010124001, 15010153006, 15010169005, 
15010228019, 15010229028, 6010001012, 6010012023, 6010014015, 
6010015008, 6020001014, 6020002037), sqr = c(14, 9, 2, 21, 13, 
26, 17.2, 21.7, 4.7, 32.2, 36.1), por = c("alpin", "alpin", "alpin", 
"alpin", "alpin", "Yornik birch", "Yornik birch", "Yornik birch", 
"Yornik birch", "Yornik birch", "Yornik birch"), zap = c(2100, 
1100, 1700, 1000, 1300, 200, 197.6744186, 170.5069124, 212.7659574, 
301.242236, 398.8919668), zappor = c(1260, 330, 850, 1000, 910, 
200, 197.6744186, 170.5069124, 212.7659574, 301.242236, 398.8919668
), zapvyd = c(2940L, 990L, 340L, 2100L, 1690L, 520L, 340L, 370L, 
100L, 970L, 1440L), coef = c(6L, 3L, 5L, 10L, 7L, 10L, 10L, 10L, 
10L, 10L, 10L), age = c(130L, 100L, 130L, 150L, 120L, 15L, 15L, 
10L, 15L, 20L, 20L), vys = c(21L, 17L, 19L, 17L, 18L, 2L, 2L, 
1L, 2L, 2L, 2L), diam = c(26L, 18L, 24L, 28L, 22L, 2L, 2L, 2L, 
2L, 2L, 2L), polnot = c(0.6, 0.4, 0.6, 0.4, 0.5, 0.7, 0.8, 0.7, 
0.7, 0.5, 0.6), BON = c(4L, 4L, 4L, 5L, 4L, 4L, 4L, 4L, 5L, 4L, 
4L), clust = c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L)), class = "data.frame", row.names = c(NA, 
-11L))

for each cluster of each por (categorical variable) i want to aggregate data by weighted.mean. I did so

library(dplyr)

mydat=mydat %>%
  group_by(clust+por) %>%
  summarise(across(zap:BON, 
                   weighted.mean, 
                   sqr))
mydat=as.data.frame(mydat)

and got the error

Error: Problem with `mutate ()` input `..1`.
x is a non-numeric argument for a binary operator
i Input `..1` is` clust + por`.

what i did wrong? How get such results. Here i performed mamually for each por category of each cluster

  N clust          por       zap   zappor    zapvyd
1 1     1        alpin 1708.0000 892.4000 2030.0000
2 2     2        alpin 1114.7059 965.5882 1943.2353
3 3     1 Yornik birch  199.0741 199.0741  448.3333
4 4     2 Yornik birch  304.1183 304.1183  968.5005
              coef      age              vys     diam    polnot
1             4,84 119.2000             19,2 22.96000 0.5280000
2 8.85294117647059 138.5294 17.3823529411765 25.70588 0.4382353
3               10  15.0000                2  2.00000 0.7398148
4               10  17.4604 1.77085533262936  2.00000 0.5938754
       BON
1 4.000000
2 4.617647
3 4.000000
4 4.049630

Upvotes: 0

Views: 1530

Answers (1)

Sinh Nguyen
Sinh Nguyen

Reputation: 4487

It should be , instead of +

mydat %>%
  # group by clust & por
  group_by(clust, por) %>%
  summarise(across(zap:BON, 
                   weighted.mean, 
                   sqr))

enter image description here

Upvotes: 1

Related Questions