R Summary table 百分比 summarise_at 或 _all 使用 3 个不同的函数并减少内部连接
R Summary table in percentage with summarise_at or _all using 3 different functions and reduce inner join
我在这里看到了 post:https://github.com/tidyverse/dplyr/issues/3101 并尝试使用 summarise_at,但是它不适用于 3 个函数,相反我发现使用 [= 更简单32=]。有什么方法可以减少我的代码中的内部连接吗?
df_ask<- structure(list(Member = c("API", "API", "API", "API", "API",
"KARA", "KARA", "KARA", "KARA", "KARA", "KARA", "KARA"), Year = c(2017,
2017, 2017, 2018, 2018, 2017, 2017, 2017, 2018, 2018, 2018, 2019
), Name = c("kali", "kata", "kaga", "kami", "kara", "mara", "misi",
"musu", "mate", "maki", "maku", "maji"), Response = c("declined",
"yes, change request", "declined", "yes", "no", "no response",
"yes, change request", "no response", "yes", "no", "yes", "no"
), Private = c("public", "private", "no response", "private",
"public", "public", "private", "no response", "private", "public",
"public", "private")), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
请注意,我还想 mutate 响应列,但仅针对 2017 年数据(拒绝 -> 否;是,更改请求 -> 是)并具有结果百分比。
结果应该是这样的:
df_ans <- structure(list(Member = c("API", "API", "KARA", "KARA", "KARA"
), Year = c(2017, 2018, 2017, 2018, 2019), `Total Name` = c(3,
2, 3, 3, 1), `Yes Response` = c(33.3333333333333, 33.3333333333333,
33.3333333333333, 66.6666666666667, 0), `No Response` = c(66.6666666666667,
33.3333333333333, 66.6666666666667, 33.3333333333333, 33.3333333333333
), Public = c(33.3333333333333, 33.3333333333333, 33.3333333333333,
66.6666666666667, 0), Private = c(33.3333333333333, 33.3333333333333,
33.3333333333333, 33.3333333333333, 33.3333333333333)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
到目前为止我的代码是:
respon.table<-df_ask %>%
group_by(Member, Year) %>%
# mutate(Response= replace(Response, c(Response == "no response", Response.status == "submitted"), c("NOONO","aaaa")))
summarise_all(funs(Total.Name=sum(!is.na(.)), Yes.Response=sum(Response %in% c("Yes", "Yes, change reques")),
private=sum(Private=="public")))
当然可以通过一些手动操作来获得该结果。但是,我想把它作为闪亮的 table,我希望它 运行 平滑并且不给更多的加载时间。请帮忙。
您不能使用 across()
将汇总函数应用于一组列
df_ask %>%
mutate(Yes.Response = Response %in% c("yes", "yes, change request"),
No.Response = Response %in% c( "no", "declined","no response"),
Is.Public = Private == "public",
Is.Private = Private == "private") %>%
group_by(Member, Year) %>%
summarise(Total.Name = sum( !is.na( Name )),
across( where(is.logical), ~paste0( round( 100 * sum(.) / Total.Name), "%")))
# A tibble: 5 x 7
# Groups: Member [2]
Member Year Total.Name Yes.Response No.Response Is.Public Is.Private
<chr> <dbl> <int> <chr> <chr> <chr> <chr>
1 API 2017 3 33% 67% 33% 33%
2 API 2018 2 50% 50% 50% 50%
3 KARA 2017 3 33% 67% 33% 33%
4 KARA 2018 3 67% 33% 67% 33%
5 KARA 2019 1 0% 100% 0% 100%
我在这里看到了 post:https://github.com/tidyverse/dplyr/issues/3101 并尝试使用 summarise_at,但是它不适用于 3 个函数,相反我发现使用 [= 更简单32=]。有什么方法可以减少我的代码中的内部连接吗?
df_ask<- structure(list(Member = c("API", "API", "API", "API", "API",
"KARA", "KARA", "KARA", "KARA", "KARA", "KARA", "KARA"), Year = c(2017,
2017, 2017, 2018, 2018, 2017, 2017, 2017, 2018, 2018, 2018, 2019
), Name = c("kali", "kata", "kaga", "kami", "kara", "mara", "misi",
"musu", "mate", "maki", "maku", "maji"), Response = c("declined",
"yes, change request", "declined", "yes", "no", "no response",
"yes, change request", "no response", "yes", "no", "yes", "no"
), Private = c("public", "private", "no response", "private",
"public", "public", "private", "no response", "private", "public",
"public", "private")), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
请注意,我还想 mutate 响应列,但仅针对 2017 年数据(拒绝 -> 否;是,更改请求 -> 是)并具有结果百分比。
结果应该是这样的:
df_ans <- structure(list(Member = c("API", "API", "KARA", "KARA", "KARA"
), Year = c(2017, 2018, 2017, 2018, 2019), `Total Name` = c(3,
2, 3, 3, 1), `Yes Response` = c(33.3333333333333, 33.3333333333333,
33.3333333333333, 66.6666666666667, 0), `No Response` = c(66.6666666666667,
33.3333333333333, 66.6666666666667, 33.3333333333333, 33.3333333333333
), Public = c(33.3333333333333, 33.3333333333333, 33.3333333333333,
66.6666666666667, 0), Private = c(33.3333333333333, 33.3333333333333,
33.3333333333333, 33.3333333333333, 33.3333333333333)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
到目前为止我的代码是:
respon.table<-df_ask %>%
group_by(Member, Year) %>%
# mutate(Response= replace(Response, c(Response == "no response", Response.status == "submitted"), c("NOONO","aaaa")))
summarise_all(funs(Total.Name=sum(!is.na(.)), Yes.Response=sum(Response %in% c("Yes", "Yes, change reques")),
private=sum(Private=="public")))
当然可以通过一些手动操作来获得该结果。但是,我想把它作为闪亮的 table,我希望它 运行 平滑并且不给更多的加载时间。请帮忙。
您不能使用 across()
将汇总函数应用于一组列
df_ask %>%
mutate(Yes.Response = Response %in% c("yes", "yes, change request"),
No.Response = Response %in% c( "no", "declined","no response"),
Is.Public = Private == "public",
Is.Private = Private == "private") %>%
group_by(Member, Year) %>%
summarise(Total.Name = sum( !is.na( Name )),
across( where(is.logical), ~paste0( round( 100 * sum(.) / Total.Name), "%")))
# A tibble: 5 x 7
# Groups: Member [2]
Member Year Total.Name Yes.Response No.Response Is.Public Is.Private
<chr> <dbl> <int> <chr> <chr> <chr> <chr>
1 API 2017 3 33% 67% 33% 33%
2 API 2018 2 50% 50% 50% 50%
3 KARA 2017 3 33% 67% 33% 33%
4 KARA 2018 3 67% 33% 67% 33%
5 KARA 2019 1 0% 100% 0% 100%