R 计算组的总值,同时保留数据框中其他列的信息
我有一个如下结构的数据帧R 计算组的总值,同时保留数据框中其他列的信息,r,dataframe,data.table,dplyr,R,Dataframe,Data.table,Dplyr,我有一个如下结构的数据帧 # Create example data ex_df <- data.frame( Date = as.Date(c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-03", "2000-01-04", "2000-01-04", "2000-01-05", "2000-01-05", "2000-01-05")), Valu
# Create example data
ex_df <- data.frame(
Date = as.Date(c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-03",
"2000-01-04", "2000-01-04", "2000-01-05", "2000-01-05",
"2000-01-05")),
Value = c(1, 3, 1, 2, 5, 2, 1, 3, 1),
Label = c("A", "B", "A", "A",
"B", "A", "A", "A", "B"),
stringsAsFactors = FALSE)
ex_df
# Date Value Label
# 1 2000-01-01 1 A
# 2 2000-01-02 3 B
# 3 2000-01-03 1 A
# 4 2000-01-03 2 A
# 5 2000-01-04 5 B
# 6 2000-01-04 2 A
# 7 2000-01-05 1 A
# 8 2000-01-05 3 A
# 9 2000-01-05 1 B
在这个数据框中,我希望Label
为A
或B
,如果来自同一日期的所有行都具有相同的标签,例如2000-01-03
中的情况。但是,如果来自同一日期的行具有不同的标签,我希望标签为B
,例如2000-01-04
和2000-01-05
中的情况
我知道我可以使用dplyr
包计算组总值,如下所示
library(dplyr)
ex_df %>%
group_by(Date) %>%
summarise(Value = sum(Value))
但是如何将信息保存在标签
列中?我欢迎不仅使用dplyr
,而且使用data.table
、base R或其他软件包的解决方案
谢谢。您可以检查每个组的标签中是否有B
,如果是,请在摘要中返回B
,否则返回A
:
library(dplyr)
ex_df %>% group_by(Date) %>%
summarise(Value = sum(Value), Label = if("B" %in% Label) "B" else "A")
# A tibble: 5 × 3
# Date Value Label
# <date> <dbl> <chr>
#1 2000-01-01 1 A
#2 2000-01-02 3 B
#3 2000-01-03 3 A
#4 2000-01-04 7 B
#5 2000-01-05 5 B
库(dplyr)
ex_df%>%集团截止日期%>%
总结(值=总和(值),标签=如果(“B”%in%Label)“B”else“A”)
#一个tibble:5×3
#日期值标签
#
#1 2000-01-01 1A
#2 2000-01-02 3 B
#3 2000-01-03 3 A
#4 2000-01-04 7 B
#5 2000-01-05 5 B
您可以检查每个组的标签中是否有B
,如果是,请在摘要中返回B
,否则返回A
:
library(dplyr)
ex_df %>% group_by(Date) %>%
summarise(Value = sum(Value), Label = if("B" %in% Label) "B" else "A")
# A tibble: 5 × 3
# Date Value Label
# <date> <dbl> <chr>
#1 2000-01-01 1 A
#2 2000-01-02 3 B
#3 2000-01-03 3 A
#4 2000-01-04 7 B
#5 2000-01-05 5 B
库(dplyr)
ex_df%>%集团截止日期%>%
总结(值=总和(值),标签=如果(“B”%in%Label)“B”else“A”)
#一个tibble:5×3
#日期值标签
#
#1 2000-01-01 1A
#2 2000-01-02 3 B
#3 2000-01-03 3 A
#4 2000-01-04 7 B
#5 2000-01-05 5 B
另一种方法是在标签上使用max
ex_df %>%
group_by(Date) %>%
summarise(Value = sum(Value),label=max(Label))
# A tibble: 5 x 3
Date Value label
<date> <dbl> <chr>
1 2000-01-01 1 A
2 2000-01-02 3 B
3 2000-01-03 3 A
4 2000-01-04 7 B
5 2000-01-05 5 B
ex_df%>%
分组单位(日期)%>%
总结(值=总和(值),标签=最大值(标签))
#一个tibble:5x3
日期值标签
1 2000-01-01 1A
2 2000-01-02 3 B
3 2000-01-03 3 A
4 2000-01-04 7 B
5 2000-01-05 5 B
另一种方法是在标签上使用max
ex_df %>%
group_by(Date) %>%
summarise(Value = sum(Value),label=max(Label))
# A tibble: 5 x 3
Date Value label
<date> <dbl> <chr>
1 2000-01-01 1 A
2 2000-01-02 3 B
3 2000-01-03 3 A
4 2000-01-04 7 B
5 2000-01-05 5 B
ex_df%>%
分组单位(日期)%>%
总结(值=总和(值),标签=最大值(标签))
#一个tibble:5x3
日期值标签
1 2000-01-01 1A
2 2000-01-02 3 B
3 2000-01-03 3 A
4 2000-01-04 7 B
5 2000-01-05 5 B
这是一个包含数据的解决方案。表
:
library("data.table")
# Create example data
ex_df <- data.table(
Date = as.Date(c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-03",
"2000-01-04", "2000-01-04", "2000-01-05", "2000-01-05",
"2000-01-05")),
Value = c(1, 3, 1, 2, 5, 2, 1, 3, 1),
Label = c("A", "B", "A", "A", "B", "A", "A", "A", "B"),
stringsAsFactors = FALSE)
foo <- function(x) { u <- unique(x); ifelse(length(u)==1, u, "B") }
ex_df[, .(Value=sum(Value), Label=foo(Label)), by=Date]
# Date Value Label
# 1: 2000-01-01 1 A
# 2: 2000-01-02 3 B
# 3: 2000-01-03 3 A
# 4: 2000-01-04 7 B
# 5: 2000-01-05 5 B
下面是一个包含数据的解决方案。表:
library("data.table")
# Create example data
ex_df <- data.table(
Date = as.Date(c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-03",
"2000-01-04", "2000-01-04", "2000-01-05", "2000-01-05",
"2000-01-05")),
Value = c(1, 3, 1, 2, 5, 2, 1, 3, 1),
Label = c("A", "B", "A", "A", "B", "A", "A", "A", "B"),
stringsAsFactors = FALSE)
foo <- function(x) { u <- unique(x); ifelse(length(u)==1, u, "B") }
ex_df[, .(Value=sum(Value), Label=foo(Label)), by=Date]
# Date Value Label
# 1: 2000-01-01 1 A
# 2: 2000-01-02 3 B
# 3: 2000-01-03 3 A
# 4: 2000-01-04 7 B
# 5: 2000-01-05 5 B
对于这种特殊情况,有Label=max(Label)
。只要标签是一个适当的有序因子,它就可以更普遍地工作(也许)。(哦,刚才看到P在回答中显示了第一个选项。)@Frank Agreemax
在这里是一个很好且简洁的选项。对于这个特定的例子,有Label=max(Label)
。只要标签是一个适当的有序因子,它就可以更普遍地工作(也许)。(哦,刚才看到P在回答中显示了第一个选项。)@Frank Agreemax
在这里是一个很好且简洁的选项。谢谢。简短的变体只需要一行代码,太棒了!非常感谢。简短的变体只需要一行代码,太棒了!