【问题标题】:Applying Functions to Filtered Rows in R将函数应用于 R 中的过滤行
【发布时间】:2020-07-12 20:27:00
【问题描述】:

我有一个很长的数据集,其中每一行都是该州当年的所得税率。下面以几行为例:

State   statefip year  TopRate
  <chr>      <dbl> <fct>   <dbl>
1 Alabama        1 2018     0.05
2 Alabama        1 2017     0.05
3 Alabama        1 2016     0.05
4 Alabama        1 2015     0.05
5 Alabama        1 2014     0.05
6 Alabama        1 2013     0.05
7 Alabama        1 2012     0.05
8 Alabama        1 2011     0.05
9 Alabama        1 2010     0.05
10 Alaska         2 2018     0   

我想为每一年创建一个额外的分位数(.5、.75、.80.. 等)列。因此,0.5(中位数)列将包含 2018 年所有州的中位数税率,依此类推。澄清一下,2018 年给定州的每个观察结果都将具有相同的 0.5 税率,但它们每年都会发生变化。虽然这可能看起来很奇怪,但当我必须将此数据与我以后拥有的其他数据合并时,它会帮助我。提前感谢您的帮助!

编辑:这就是我想要的..

State   statefip year  TopRate median     q3    q80    q85    q90 jenkb$jenkb
   <chr>      <dbl> <fct>   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>       <int>
 1 Alabama        1 2018     0.05 0.0575 0.0700 0.074  0.0888 0.0897           0
 2 Alabama        1 2017     0.05 0.0575 0.0700 0.074  0.0854 0.0895           0
 3 Alabama        1 2016     0.05 0.0575 0.0700 0.074  0.0854 0.0895           0
 4 Alabama        1 2015     0.05 0.0575 0.0700 0.074  0.0888 0.0897           0
 5 Alabama        1 2014     0.05 0.058  0.07   0.0765 0.0888 0.0897           0
 6 Alabama        1 2013     0.05 0.0599 0.072  0.0775 0.0839 0.0895           0
 7 Alabama        1 2012     0.05 0.06   0.072  0.0775 0.0866 0.0895           0
 8 Alabama        1 2011     0.05 0.06   0.0738 0.078  0.085  0.0897           0
 9 Alabama        1 2010     0.05 0.06   0.0738 0.078  0.0872 0.0897           1
10 Alaska         2 2018     0    0.0575 0.0700 0.074  0.0888 0.0897           0
# ... with 449 more rows

除了我希望 JenkB 列是 1 之外,该州适合该给定年份的 Jenks 休息的第二个桶。

taxlong %>%
  group_by(year) %>% 
  mutate(median = quantile(TopRate, .5)) %>%
  mutate(q3 = quantile(TopRate, .75)) %>%
  mutate(q80 = quantile(TopRate, .80)) %>%
  mutate(q85 = quantile(TopRate, .85)) %>%
  mutate(q90 = quantile(TopRate, .90)) #%>%
  #mutate(jenkb = as.integer((TopRate > 0.0323 & year == 2018 | 2017 | 2015) | (TopRate > 0.0375 & year == 2016) | (TopRate > 0.034 & year == 2014 | 2013 | 2012 | 2011 | 2010)))

此代码无效

【问题讨论】:

  • 所有值似乎都是 0.05。你可以试试df1 %&gt;% group_by(State, statefip, year) %&gt;% mutate(new = cut(TopRate, breaks = c(-Inf, quantile(TopRate), Inf)))
  • 那么,您想要每年的分位数和每个州的分位数?
  • 你能分享示例的预期输出吗?
  • @Ronak Shah 我编辑了原始帖子以添加预期的输出
  • 或许,您正在寻找mutate(jenkb = as.integer(TopRate &gt; 0.0323 &amp; year %in% c(2018,2017,2015) | (TopRate &gt; 0.0375 &amp; year == 2016) | (TopRate &gt; 0.034 &amp; year %in% 2010:2014)))

标签: r


【解决方案1】:

我们可以使用quantilecut在按组列'year'分组后创建一个列

library(dplyr)
df1 %>%
    group_by( year) %>% 
    mutate(new = cut(TopRate, breaks = c(-Inf, quantile(TopRate, 
            probs = c(.5, .75, .8) ), Inf)))

如果我们想创建具有quantile 值的新列(每行将重复值),请将其置于list 中,然后将unnest 置于“宽”格式

library(tidyr)
df1 %>% 
   group_by(year) %>%
   mutate(new = list(quantile(TopRate, probs = c(.5, .75, .8)))) %>% 
   unnest_wider(c(new))

data.table

library(data.table)
setDT(df1)[, new := cut(TopRate, breaks = c(-Inf, quantile(TopRate, probs = c(.5, .75, .8)), Inf)), .( year)]

【讨论】:

  • 这行得通,但我不希望它们仅按年按州和 statefip 分组(因此 2018 年的所有州都具有相同的 2018 年中值)。但是现在我的中值是从-inf到中值的范围```State statefip year TopRate new 1 Alabama 1 2018 0.05 (-Inf,0.0575]
  • @MattSimonson 通过更改分组列更新了帖子
  • 有没有一种简单的方法来获得它,所以值只是中位数而不是范围?
  • 我将为每个百分位数设置一个单独的列
  • @MattSimonson 如果你只想有两个组,你不需要cutmutate(new = as.integer(TopRate &gt;= median(TopRate, na.rm = TRUE)))
【解决方案2】:

感谢您的帮助。

taxlong %>%
  group_by(year) %>% 
  mutate(median = quantile(TopRate, .5))

完美运行,我将为我需要的每个分位数重复该过程。我需要保留这些值以供以后分析。

【讨论】:

    猜你喜欢
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    相关资源
    最近更新 更多