R中多列按组计算百分比

Ziz*_*zou 0 r transform percentage dataframe

我有几个包含每月数据的数据框,我想找到每个产品和每个月的百分比分布。我对几个月的多列有问题。目前,我只能获得一个月内按组划分的百分比。

data <- data.frame(group = rep(LETTERS[1:3], each = 4),  
                   Product = letters[1:4],
                   January = sample(1:100,12),
                   February = sample(1:100,12))

data_new1 <- transform(data,                             
                       perc = ave(January,
                                  group,
                                  FUN = prop.table))
data_new1$perc<-round(data_new1$perc, 2)

> data_new1
   group Product January February perc
1      A       a      12       16 0.05
2      A       b      73       75 0.32
3      A       c      78       11 0.34
4      A       d      65       35 0.29
5      B       a      86       63 0.36
6      B       b      33       71 0.14
7      B       c      92       49 0.38
8      B       d      30       60 0.12
9      C       a      91       59 0.37
10     C       b      31       45 0.12
11     C       c      99        7 0.40
12     C       d      28       50 0.11
Run Code Online (Sandbox Code Playgroud)

Yur*_*kin 6

整洁宇宙

library(dplyr)
data %>% 
  group_by(group) %>% 
  mutate(across(c("January", "February"), proportions, .names = "{.col}_perc")) %>% 
  ungroup()

# A tibble: 12 x 6
   group Product January February January_perc February_perc
   <chr> <chr>     <int>    <int>        <dbl>         <dbl>
 1 A     a            49       40      0.426          0.252 
 2 A     b             1        3      0.00870        0.0189
 3 A     c            19       50      0.165          0.314 
 4 A     d            46       66      0.4            0.415 
 5 B     a            61       82      0.218          0.285 
 6 B     b            88       51      0.314          0.177 
 7 B     c            32       75      0.114          0.260 
 8 B     d            99       80      0.354          0.278 
 9 C     a             6       31      0.0397         0.373 
10 C     b             8        5      0.0530         0.0602
11 C     c            92       20      0.609          0.241 
12 C     d            45       27      0.298          0.325 
Run Code Online (Sandbox Code Playgroud)

根据

library(dplyr)
data %>% 
  group_by(group) %>% 
  mutate(across(c("January", "February"), proportions, .names = "{.col}_perc")) %>% 
  ungroup()

# A tibble: 12 x 6
   group Product January February January_perc February_perc
   <chr> <chr>     <int>    <int>        <dbl>         <dbl>
 1 A     a            49       40      0.426          0.252 
 2 A     b             1        3      0.00870        0.0189
 3 A     c            19       50      0.165          0.314 
 4 A     d            46       66      0.4            0.415 
 5 B     a            61       82      0.218          0.285 
 6 B     b            88       51      0.314          0.177 
 7 B     c            32       75      0.114          0.260 
 8 B     d            99       80      0.354          0.278 
 9 C     a             6       31      0.0397         0.373 
10 C     b             8        5      0.0530         0.0602
11 C     c            92       20      0.609          0.241 
12 C     d            45       27      0.298          0.325 
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v2.0.1)于 2021 年 12 月 20 日创建

数据表

library(data.table)

COLS <- c("January", "February")
COLS_RES <- paste0(COLS, "_perc")
setDT(data)[, (COLS_RES) := lapply(.SD, proportions), by = group, .SDcol = COLS][]
Run Code Online (Sandbox Code Playgroud)