gpt4 book ai didi

r - 在 R 中的同一列中获取计数和总和

转载 作者:行者123 更新时间:2023-12-02 00:53:56 26 4
gpt4 key购买 nike

我在下面提到了 R 中的数据框。

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 , BC每个日期都应该是静态的,无论该特定类型在该日期是否可用。

另外,我要数 ID按日期分组和 Type , 在 <5 的桶中(如果值在 1-4 之间), 5-10 (如果值在 5 到 10 之间)和 >10 (如果值高于 10)。
sum列应包含该特定日期和类型的总值(value)。
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

最佳答案

表格包非常适合紧凑地描述此类输出。首先创建计算列中显示的统计信息的函数。然后使用指示的 tabular公式。 LHS 是行,RHS 是列。 + 表示连接在 + 两侧描述的变量。
sprintf的输出例如,可以通过更改格式字符串来改变。见 ?sprintf .

latex

还有如果 tabtabular 的输出然后命令 latex(tab)将创建一个 latex 版本,您可以通过插入 latex 命令进一步改变它。例如 "%d \\tiny{(%d)}"sprintf格式字符串将使 latex 输出中括号内的部分变小。

html

如果你想要 html然后输出 tab正如刚刚定义的那样 html(tab)创建一个 html 版本,可以使用适当的 html 标签进一步改变。例如 "%d <small>(%d)</small>"sprintf格式字符串将使 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 0

关于r - 在 R 中的同一列中获取计数和总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55676920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com