下面我在R中提到了dataframe。
ID Date Type Value
K-1 2018-01-01 A 4
K-2 2018-01-01 B 7
K-3 2018-01-01 C 12
K-4 2018-01-02 A 6
K-5 2018-01-02 A 4
K-6 2018-01-02 B 15
K-7 2018-01-02 B 10我想学习如何在下面给定所需的数据帧的情况下转换数据帧,其中A、B和C对于每个日期都应该是静态的,无论该特定类型在该日期是否可用。
另外,我想要在<5 (如果值在1-4之间),5-10 (如果值在5到10之间)和>10 (如果值大于10)的存储桶中,按date和Type计算ID分组。
sum列还应包含该特定日期和类型的值的总和。
Count列应包含按特定日期分组的ID计数和Type。
存储桶<5、5-10和>10应始终出现在所需输出中,而不管该存储桶的值是否可用。
此外,如何根据特定ID组在括号()中的存储桶求和,并用逗号分隔2个十进制值。括号中sum的字体应小于count的字体(即如果<5存储桶的count字体为12,则括号中sum的字体应为10)。此外,如果特定存储桶中的计数为0,则不需要使用括号(0.00)作为值。
所需的DF
Date Count <5 5-10 >10 sum
2018-01-01 3 1 (4) 1 (7) 1 (12) 23
A 1 1 (4) 0 0 4
B 1 0 1 (7) 0 7
C 1 0 0 1 (12) 12
2018-01-02 4 1 (4) 2 (16) 1 (15) 35
A 2 1 (4) 1 (6) 0 10
B 2 0 1 (10) 1 (15) 25
C 0 0 0 0 0我使用的代码(来自SO):
library(tidyverse)
dat2 <- dat %>%
mutate(Result = case_when(
Value < 5 ~"<5",
Value >= 5 & Value <= 10 ~"5-10",
Value > 10 ~">10"
)) %>%
group_by(Date, Type, Result) %>%
summarize(sum = sum(Value)) %>%
mutate(Flag = 1L) %>%
spread(Result, Flag, fill = 0L) %>%
group_by(Date, Type) %>%
summarize_all(list(~sum(.))) %>%
ungroup() %>%
complete(Date, Type)
dat2[is.na(dat2)] <- 0
dat3 <- dat2 %>% mutate(Count = rowSums(select(., -Date, -Type, -sum)))
dat4 <- dat3 %>%
group_by(Date) %>%
summarize_at(vars(-Type), list(~sum(.)))
dat_final <- map2_dfr(split(dat4, f = dat4$Date),
split(dat3, f = dat3$Date),
~bind_rows(.x %>% rename(Type = Date),
.y %>% select(-Date)))
dat_final2 <- dat_final %>%
select(Date = Type, Count, `<5`, `5-10`, `>10`, sum)
dat_final2发布于 2019-04-15 00:44:13
tables包对于简洁地描述这样的输出是很好的。首先,创建计算列中显示的统计数据的函数。然后使用指示的tabular公式。LHS是行,RHS是列。+表示连接+两边描述的变量。
例如,可以通过改变格式串来改变sprintf的输出。参见?sprintf。
乳胶
另外,如果tab是tabular命令的输出,那么latex(tab)将创建一个latex版本,您可以通过插入latex命令进一步更改它。例如,作为sprintf格式字符串的"%d \\tiny{(%d)}"将使latex输出中带括号的部分更小。
html
如果您想要html输出,那么使用刚才定义的tab,那么html(tab)将创建一个html版本,该版本可以通过适当的html标记进一步改变。例如,作为sprintf格式字符串的"%d <small>(%d)</small>"将使带括号的部分在html输出中变小。
输入
我们在末尾的注释中以可重现的形式提供了输入dat。下次请确保输入以可重现的形式提供。
代码
这主要重现了问题中显示的输出,并且比那里的代码紧凑得多。
library(tables)
outstring <- function(x) if (length(x)) sprintf("%d (%d)", length(x), sum(x)) else 0
`<5` <- function(x) outstring(x[x < 5])
`5-10` <- function(x) outstring(x[x >= 5 & x <= 10])
`>10` <- function(x) outstring(x[x > 10])
tab <-
tabular(Date * (1 + Type) ~ (n=1) + Value * (`<5` + `5-10` + `>10` + sum), data = dat)给予:
Value
Date n <5 5-10 >10 sum
2018-01-01 All 3 1 (4) 1 (7) 1 (12) 23
Type A 1 1 (4) 0 0 4
B 1 0 1 (7) 0 7
C 1 0 0 1 (12) 12
2018-01-02 All 4 1 (4) 2 (16) 1 (15) 35
Type A 2 1 (4) 1 (6) 0 10
B 2 0 1 (10) 1 (15) 25
C 0 0 0 0 0 备注
dat <-
structure(list(ID = structure(1:7, .Label = c("K-1", "K-2", "K-3",
"K-4", "K-5", "K-6", "K-7"), class = "factor"), Date = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 2L), .Label = c("2018-01-01", "2018-01-02"
), class = "factor"), Type = structure(c(1L, 2L, 3L, 1L, 1L,
2L, 2L), .Label = c("A", "B", "C"), class = "factor"), Value = c(4L,
7L, 12L, 6L, 4L, 15L, 10L)), class = "data.frame", row.names = c(NA,
-7L))更新
tabular类有一个as.matrix方法,我们可以对其执行简单的操作,以产生以下输出:
m <- as.matrix(tab)
m2 <- cbind(paste0(m[, 1], sub("All", "", m[, 3])), m[, -(1:3)])[-1, ]
setNames(as.data.frame(m2[-1, ]), m2[1, ])给予:
Date n <5 5-10 >10 sum
1 2018-01-01 3 1 (4) 1 (7) 1 (12) 23
2 A 1 1 (4) 0 0 4
3 B 1 0 1 (7) 0 7
4 C 1 0 0 1 (12) 12
5 2018-01-02 4 1 (4) 2 (16) 1 (15) 35
6 A 2 1 (4) 1 (6) 0 10
7 B 2 0 1 (10) 1 (15) 25
8 C 0 0 0 0 0https://stackoverflow.com/questions/55676920
复制相似问题