【问题标题】:R divide the value of a variable by the number of its occurrencesR将变量的值除以其出现的次数
【发布时间】:2022-11-12 15:59:07
【问题描述】:

我有一个数据框,其中变量value 的数字是适用于整个特定区域的特定项目的总资金数额。由于资助金额是作为总额报告的,因此无法确定一个地区收到的单独金额,因此会显示该计划涵盖的每个地区的总金额。因此,我需要根据每个区域所划分的区域数量来平均分配给每个区域的数量。我怎样才能为每个程序做到这一点?

我的数据框如下所示:

program region value                                                                        
a        01    100
b        02    250
b        03    250
b        04    250
c        01    200
c        03    200
d        02    600
e        01    700
f        01    100
f        04    100

因此,所需的输出如下:

program region value new_value                                                                      
a        01    100     100
b        02    250     83.333
b        03    250     83.333
b        04    250     83.333
c        01    200     100
c        03    200     100
d        02    600     600
e        01    700     700
f        01    100     50
f        04    100     50

【问题讨论】:

    标签: r


    【解决方案1】:

    如果 df 是您的原始数据框

    library(tidyverse)
    
    df %>%
      group_by(program) %>%
      mutate(new_value = first(value) / n())
    

    【讨论】:

      【解决方案2】:

      这是一个 R 基础解决方案

      > df$new_value <- with(df, ave(value, program, FUN= function(x) x/length(x)))
      > df
         program region value new_value
      1        a      1   100 100.00000
      2        b      2   250  83.33333
      3        b      3   250  83.33333
      4        b      4   250  83.33333
      5        c      1   200 100.00000
      6        c      3   200 100.00000
      7        d      2   600 600.00000
      8        e      1   700 700.00000
      9        f      1   100  50.00000
      10       f      4   100  50.00000
      

      【讨论】:

        【解决方案3】:

        使用data.table

        library(data.table)
        setDT(df)[, new_value := first(value)/.N, program]
        

        -输出

        > df
            program region value new_value
             <char>  <int> <int>     <num>
         1:       a      1   100 100.00000
         2:       b      2   250  83.33333
         3:       b      3   250  83.33333
         4:       b      4   250  83.33333
         5:       c      1   200 100.00000
         6:       c      3   200 100.00000
         7:       d      2   600 600.00000
         8:       e      1   700 700.00000
         9:       f      1   100  50.00000
        10:       f      4   100  50.00000
        

        数据

        df <- structure(list(program = c("a", "b", "b", "b", "c", "c", "d", 
        "e", "f", "f"), region = c(1L, 2L, 3L, 4L, 1L, 3L, 2L, 1L, 1L, 
        4L), value = c(100L, 250L, 250L, 250L, 200L, 200L, 600L, 700L, 
        100L, 100L)), class = "data.frame", row.names = c(NA, -10L))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-18
          • 1970-01-01
          • 1970-01-01
          • 2014-07-23
          • 2020-06-13
          • 2016-04-20
          • 1970-01-01
          相关资源
          最近更新 更多