【问题标题】:How do I use a filter within a mutate statement?如何在 mutate 语句中使用过滤器?
【发布时间】:2019-03-16 06:43:59
【问题描述】:

我想计算特定日期与公司列表相关的投资。我有公司名单和投资日期。

这是我的数据-

参考日期

d1 <- as.Date(paste0("201001","01"), "%Y%m%d")
d2 <- as.Date(paste0("201201","01"), "%Y%m%d")
dat <- seq(d1,d2,by="month")

投资数据

> head(df)
        company_name funding_round_type funding_round_code  funded_at raised_amount_usd  yearMonth
1            0-6.com            venture                  A 2008-03-19           2000000 2008-03-01
2   004 Technologies            venture                    2014-07-24                NA 2014-03-01
3 01Games Technology        undisclosed                    2014-07-01             41250 2014-03-01
4             H2O.ai            venture                  B 2015-11-09          20000000 2015-03-01
5             H2O.ai               seed                    2013-05-22           3000000 2013-03-01
6             H2O.ai            venture                    2013-01-03           1700000 2013-03-01

我想计算每个公司在dat 中的每个日期时筹集了多少资金。

result <- merge(dat, df$company_name) %>% 
  mutate(asOf = x,
         companyName = as.character(y)) %>% select(-x, -y) %>%
  mutate(raised = sum(df[df$company_name == companyName & 
                                      df$yearMonth < asOf,c("raised_amount_usd")])) 

很遗憾,过滤器无法正常工作。如果我将其设置为特定的公司,它就可以工作。这有效,例如:sum(df[df$company_name == companyName &amp; df$yearMonth &lt; asOf,c("raised_amount_usd")])

我想得到一个看起来像这样的结果 -

        asOf                      companyName            cumulative_raised
1            2010-01-01            0-6.com                  0
2            2010-02-01            0-6.com                  12000000
3            2010-03-01            0-6.com                  12000000
4            2010-01-01            H2O.ai                   0
5            2010-02-01            H2O.ai                   5000000
6            2010-03-01            H2O.ai                   9300000

如何让过滤器在 mutate 子句中工作?

【问题讨论】:

  • 请描述问题而不是解决方案。这让人误解,你到底想做什么?见the X-Y problemfilter 可能被解释为引用 filter 函数。
  • 当然,我更新了它。

标签: r dplyr


【解决方案1】:

解决这个问题的一种方法是使用complete(来自tidyr)和group_bymutatesummarize(来自dplyr)以及cumsum而不是sum(基地R)。 由于您提供的数据与您想要的间隔几乎没有重叠,我稍微修改了间隔以显示它是如何工作的。当然,这是完全灵活的,你可以使用任何你想要的间隔:

library(dplyr)
library(tidyr)

my.dat <- seq(as.Date("2013-03-01"), as.Date("2014-04-01"), by = "month")

new.df <- my.df %>% 
  complete(company_name, yearMonth = my.dat, fill = list(raised_amount_usd = 0)) %>% 
  group_by(company_name, yearMonth) %>% 
  summarize(raised_amount_usd = sum(raised_amount_usd, na.rm = TRUE)) %>% 
  arrange(yearMonth) %>% 
  mutate(cumulative_raised = cumsum(raised_amount_usd)) %>% 
  select(company_name, yearMonth, cumulative_raised) 

tail(new.df, 10)

# A tibble: 10 x 3
# Groups:   company_name [4]
   company_name       yearMonth  cumulative_raised
   <chr>              <date>                 <dbl>
 1 01Games Technology 2014-02-01                 0
 2 H2O.ai             2014-02-01           4700000
 3 0-6.com            2014-03-01                 0
 4 004 Technologies   2014-03-01                 0
 5 01Games Technology 2014-03-01             41250
 6 H2O.ai             2014-03-01           4700000
 7 0-6.com            2014-04-01                 0
 8 004 Technologies   2014-04-01                 0
 9 01Games Technology 2014-04-01             41250
10 H2O.ai             2014-04-01           4700000

它是如何工作的?

首先,我们用complete 填写yearMonth 列中缺少的日期,并排除不在指定时间范围内的日期。然后使用group_by,我们形成company_nameyearMonth 组,然后summarizeraised_amount_usd 用于每个日期和公司(将在2013 年与H2O.ai 同一日期筹集的金额相加-03-01)。然后我们将数据按yearMonth排列并计算累积和。数据仍按company_name 分组,因此计算每家公司的累计总和。最后,我们只选择您感兴趣的那些列。

数据

my.df <- 
  structure(list(company_name = c("0-6.com", "004 Technologies", "01Games Technology", "H2O.ai", "H2O.ai", "H2O.ai"), 
                 funding_round_type = c("venture", "venture", "undisclosed", "venture", "seed", "venture"), 
                 funding_round_code = c("A", " ", " ", "B", " ", " "), 
                 funded_at = structure(c(13957, 16275, 16252, 16748, 15847, 15708), class = "Date"), 
                 raised_amount_usd = c(2000000L, NA, 41250L, 20000000L, 3000000L, 1700000L), 
                 yearMonth = structure(c(13939, 16130, 16130, 16495, 15765, 15765), class = "Date")), 
            class = "data.frame", row.names = c(NA, -6L))

【讨论】:

  • 这可以解决问题。谢谢凯丝。您知道为什么 companyName 不能用作过滤器吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
相关资源
最近更新 更多