优化 R 中输出表生成的代码
Posted
技术标签:
【中文标题】优化 R 中输出表生成的代码【英文标题】:Optimize code for output table generation in R 【发布时间】:2022-01-21 19:15:00 【问题描述】:如何将这 4 个代码片段合并到一个管道?
library(tidyverse)
library(lubridate)
v1<- df1 %>%
select(starts_with("DR0")) %>% names %>%
paste0("coef-",.)
All<-left_join(All, df1, by = c("date2", "Category")) %>%
mutate(across(starts_with("DR0"), ~ coef - .)) %>%
select(-Week, -DR1) %>%
rename_at(-c(1:4), ~v1)%>%
relocate(date1)
All<-All %>%
mutate(across(date1:date2, as.Date)) %>%
pivot_longer(starts_with('coef-'), values_to = 'Result') %>%
filter(date2 - date1 == as.numeric(str_sub(name, -2))) %>%
select(-date1,-name)
All<-data.frame(All)
我的数据:
df1 <- structure(list(date1 = c("2021-06-28", "2021-06-28", "2021-06-28", "2021-06-28"),
date2 = c("2021-06-30", "2021-06-30", "2021-07-01", "2021-07-01"),
Category = c("FDE", "ABC", "FDE", "ABC"),
Week = c("Wednesday", "Wednesday", "Friday", "Friday"),
DR1 = c(4, 1, 6, 3), DR01 = c(4, 1, 4, 3), DR02 = c(4, 2, 6, 2),
DR03 = c(9, 5, 4, 7), DR04 = c(5, 4, 3, 2), DR05 = c(5, 4, 5, 4),
DR06 = c(2, 4, 3, 2)), class = "data.frame", row.names = c(NA, -4L))
All <- structure(list(date2 = c("2021-06-30", "2021-06-30", "2021-07-01", "2021-07-01"),
Category = c("FDE", "ABC", "FDE", "ABC"), coef = c(4L, 1L, 6L, 3L)),
class = "data.frame", row.names = c("1", "2", "3", "4"))
预期输出:
> All
date2 Category coef Result
1 2021-06-30 FDE 4 0
2 2021-06-30 ABC 1 -1
3 2021-07-01 FDE 6 2
4 2021-07-01 ABC 3 -4
【问题讨论】:
感谢您的回复!是否可以将您的代码作为答案? 我现在明白了。感谢您的回答,如果问题重新打开,我接受您的回答。 【参考方案1】:La respuesta es de TarJae:
library(tidyverse)
library(lubridate)
All_new <-
left_join(All, df1, by = c("date2", "Category")) %>%
mutate(across(starts_with("DR0"), ~ coef - .), across(contains("date"), ymd)) %>%
select(-Week,-DR1) %>%
rename_with( ~ paste0("coef-", .), starts_with("DR0")) %>%
pivot_longer(starts_with('coef-'), values_to = 'Result') %>%
filter(date2 - date1 == as.numeric(str_sub(name,-2))) %>%
select(-date1, -name) %>%
data.frame()
【讨论】:
【参考方案2】:我们可以这样做:
library(tidyverse)
library(lubridate)
All_new <- left_join(All, df1, by = c("date2", "Category")) %>%
mutate(across(starts_with("DR0"), ~ coef - .), across(contains("date"), ymd)) %>%
select(-Week, -DR1) %>%
rename_with(~ paste0("coef-", .), starts_with("DR0")) %>%
pivot_longer(starts_with('coef-'), values_to = 'Result') %>%
filter(date2 - date1 == as.numeric(str_sub(name, -2))) %>%
select(-date1,-name) %>%
data.frame()
【讨论】:
以上是关于优化 R 中输出表生成的代码的主要内容,如果未能解决你的问题,请参考以下文章
R语言使用table1包绘制(生成)三线表使用单变量分列构建三线表编写自定义三线表结构(将因子变量细粒度化重新构建三线图)自定义修改描述性统计参数输出自定义统计量
R语言获得所有Aesthetics(美学映射)参数:使用长表输出使用宽表输出
R语言table函数和xtabs函数生成三维列联表使用margin.table函数prop.table函数和addmargins函数计算多维列联表的边缘频率值频率计数比例值ftable美化输出