Reputation: 1132
I have tried to to counts of two numerical variables and did not succeed. Without this I cannot achieve the Percentages, which I hope I am able to get with your help. I am trying to do this only with tidyverse.
This is the error I got, with the code provided:
test_sum <- test_data_3 %>%
dplyr::group_by(across(where(is.factor))) %>%
dplyr::summarise(across(where(is.numeric())))
Error: Problem with `summarise()` input `..1`.
ℹ `..1 = across(where(is.numeric()))`.
x 0 arguments passed to 'is.numeric' which requires 1
Run `rlang::last_error()` to see where the error occurred.
The I tried another code:
test_sum <- test_data_3 %>%
dplyr::group_by(provider_name, type, st_nst) %>%
dplyr::summarise(across(where(is.numeric())))
Error: Problem with `summarise()` input `..1`.
ℹ `..1 = across(where(is.numeric()))`.
x 0 arguments passed to 'is.numeric' which requires 1
ℹ The error occurred in group 1: provider_name = "BLACKB", type = "stri", st_nst = "NST".
And this is the stack overflow source I have inspired to try the previous codes: Group by multiple columns and sum other multiple columns
And this is the type of data I have:
dput(test_data_3)
structure(list(financial_year = c(1920, 1920, 1920, 1920, 1920,
1920, 1920, 1920, 1920, 1920, 1920, 1920, 1920, 1920, 1920, 1920,
1920, 1920, 1920, 1920), provider_name = c("LIVEW", "MANCHE",
"MANCHE", "MANCHE", "MANCHE", "MANCHE", "MANCHE", "MANCHE", "MANCHE",
"SOUTH", "LANCA", "COUNTY", "BUCKINGT", "BLACKB", "BURNLEY",
"ROYAL", "THE", "LOUTH", "IMPERIAL", "WESTERN"), type = c("non_stringent",
"non_stringent", "non_stringent", "non_stringent", "non_stringent",
"non_stringent", "non_stringent", "non_stringent", "non_stringent",
"non_stringent", "stri", "stri", "stri", "stri", "stri", "stri",
"stri", "stri", "stri", "stri"), eld = c(0, 326, 343, 43, 61,
46, 1, 3, 3, 1, 313, 671, 329, 389, 3, 376, 306, 0, 409, 589),
ed = c(1, 23, 23, 0, 2, 0, 1, 0, 0, 0, 7, 3, 4, 4, 0, 0,
2, 1, 3, 1), st_nst = c("ST", "STI", "ST", "ST", "ST", "ST",
"ST", "ST", "ST", "ST", "NST", "NST", "NSt", "NST", "NST",
"NST", "NST", "NST", "NST", "NST")), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), spec = structure(list(
cols = list(financial_year = structure(list(), class = c("collector_double",
"collector")), trust_code = structure(list(), class = c("collector_character",
"collector")), provider_name = structure(list(), class = c("collector_character",
"collector")), prim_diag = structure(list(), class = c("collector_character",
"collector")), type = structure(list(), class = c("collector_character",
"collector")), elective_discharge = structure(list(), class = c("collector_double",
"collector")), emergency_admission = structure(list(), class = c("collector_double",
"collector")), st_nst = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
Or another way of visualising is as such:
test_data_3
# A tibble: 20 x 6
financial_year provider_name type eld ed st_nst
<dbl> <chr> <chr> <dbl> <dbl> <chr>
1 1920 LIVEW non_stringent 0 1 ST
2 1920 MANCHE non_stringent 326 23 STI
3 1920 MANCHE non_stringent 343 23 ST
4 1920 MANCHE non_stringent 43 0 ST
5 1920 MANCHE non_stringent 61 2 ST
6 1920 MANCHE non_stringent 46 0 ST
7 1920 MANCHE non_stringent 1 1 ST
8 1920 MANCHE non_stringent 3 0 ST
9 1920 MANCHE non_stringent 3 0 ST
10 1920 SOUTH non_stringent 1 0 ST
11 1920 LANCA stri 313 7 NST
12 1920 COUNTY stri 671 3 NST
13 1920 BUCKINGT stri 329 4 NSt
14 1920 BLACKB stri 389 4 NST
15 1920 BURNLEY stri 3 0 NST
16 1920 ROYAL stri 376 0 NST
17 1920 THE stri 306 2 NST
18 1920 LOUTH stri 0 1 NST
19 1920 IMPERIAL stri 409 3 NST
20 1920 WESTERN stri 589 1 NST
Can someone explain the mistakes I do? Is there a way to achieve the counts first and then the percentages of the 2 numerical columns, namely eld & ed
grouped by provider_name, type, st_nst
. I mean these two columns to be added together into a new one based on the group by variables.
Upvotes: 1
Views: 46
Reputation: 887213
There was no function passed into across
. If the intention is to select
the columns
library(dplyr)
test_data_3 %>%
dplyr::group_by(across(where(is.factor))) %>%
dplyr::select(where(is.numeric))
Suppose, we want to get the sum
of those numeric
columns
test_data_3 %>%
dplyr::group_by(across(where(is.factor))) %>%
dplyr::summarise(across(where(is.numeric), sum))
If we want to get the sum per row of numeric columns, select
the numeric
columns (where(is.numeric)
) from the data (cur_data()
- would be more correct as it can also work when there are group attributes or use .
), get the row wise sum with rowSums
test_data_3 %>%
mutate(count = select(cur_data(), where(is.numeric)) %>%
rowSums(na.rm = TRUE))
Upvotes: 1