self_function 无法工作——无法对超出末尾的列进行子集化

问题描述 投票:0回答:1

有数据框

raw_df
如下:

library(tidyverse)    
detail <- data.frame(cat=c("a","a","a","b","b","b","b","c","c"),
                         single_amount=c(1,3,7,2,1,4,6,1,6))
    
    total <- data.frame(cat=c("a","b","c"),
                        total_amount=c(20,10,9))
    
    raw_df <- detail %>% left_join(total,by='cat')

我定义了 self 函数

allocate_data_m
,当
total_amount
小于
cumsum(single_data)
 时分配 
total_amount

allocate_data_m <- function(data,single_data,total_data){
 out <- cumsum_single_data <- rep('NA',nrow(data))
  for (grouprow in seq_len(nrow(data))){
    
    cumsum_single_data[grouprow] <- 
      if_else(grouprow ==1,data[grouprow,single_data],
              cumsum_single_data[grouprow-1] + data[grouprow,single_data])
    
     out[grouprow] <- if_else(cumsum_single_data[grouprow] < data[grouprow,total_data],data[grouprow,single_data],0)
  }
    out 
}

当运行

allocate_data_m
中的
mutate
时,出现错误。有人可以帮忙吗?谢谢!

raw_df %>% group_by(cat) %>%
  mutate(amount_x_all = allocate_data_m(cur_data(),single_amount,total_amount))
r function
1个回答
0
投票

你在追求这样的东西吗:

library(dplyr, warn.conflicts = FALSE)    
detail <- data.frame(cat=c("a","a","a","b","b","b","b","c","c"),
                     single_amount=c(1,3,7,2,1,4,6,1,6))
total <- data.frame(cat=c("a","b","c"),
                    total_amount=c(20,10,9))
(raw_df <- detail %>% left_join(total,by='cat') %>% as_tibble())
#> # A tibble: 9 × 3
#>   cat   single_amount total_amount
#>   <chr>         <dbl>        <dbl>
#> 1 a                 1           20
#> 2 a                 3           20
#> 3 a                 7           20
#> 4 b                 2           10
#> 5 b                 1           10
#> 6 b                 4           10
#> 7 b                 6           10
#> 8 c                 1            9
#> 9 c                 6            9

raw_df %>% 
  # if cumsum(single_amount) < total_amount) is TRUE,  multiply with 1 (TRUE)
  # if cumsum(single_amount) < total_amount) is FALSE, multiply with 0 (FALSE)
  mutate(amount_x_all = (cumsum(single_amount) < total_amount) * total_amount, .by = cat)
#> # A tibble: 9 × 4
#>   cat   single_amount total_amount amount_x_all
#>   <chr>         <dbl>        <dbl>        <dbl>
#> 1 a                 1           20           20
#> 2 a                 3           20           20
#> 3 a                 7           20           20
#> 4 b                 2           10           10
#> 5 b                 1           10           10
#> 6 b                 4           10           10
#> 7 b                 6           10            0
#> 8 c                 1            9            9
#> 9 c                 6            9            9

创建于 2024-04-27,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.