【问题标题】:R How to apply formula to certain columnsR如何将公式应用于某些列
【发布时间】:2020-03-12 02:18:55
【问题描述】:

我有一个这样的数据框:

VisitID | No_Of_Visits | Store A | Store B | Store C | Store D|
   A1   |  1           |   1     |  0      |  0      |  0     |
   B1   |  2           |   1     |  0      |  0      |  1     |
   C1   |  4           |   1     |  2      |  1      |  0     |
   D1   |  3           |   2     |  0      |  1      |  0     |
   E1   |  4           |   1     |  1      |  1      |  1     |

在 R 中,如何转换 Dataframe 以计算每个商店类别的访问百分比,即每个商店的访问次数除以访问级别的 No_Of_Visits?预期结果:

VisitID | No_Of_Visits | Store A | Store B | Store C | Store D|
   A1   |  1           |   100%  |  0      |  0      |  0     |
   B1   |  2           |   50%   |  0      |  0      |  50%   |
   C1   |  4           |   25%   |  50%    |  25%    |  0     |
   D1   |  3           |   67%   |  0      |  33%    |  0     |
   E1   |  4           |   25%   |  25%    |  25%    |  25%   |

dplyr 有什么方法吗?还是必须使用 sapply 函数?

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    我们可以使用prop.table

    df[-c(1:2)] <- prop.table(as.matrix(df[-c(1:2)]), 1) * 100
    df
    
    #  VisitID No_Of_Visits   Store.A Store.B  Store.C Store.D
    #1      A1            1 100.00000       0  0.00000       0
    #2      B1            2  50.00000       0  0.00000      50
    #3      C1            4  25.00000      50 25.00000       0
    #4      D1            3  66.66667       0 33.33333       0
    #5      E1            4  25.00000      25 25.00000      25
    

    tidyverse中,我们可以得到长格式的数据,将值除以No_Of_Visits,得到宽格式的数据:

    library(dplyr)
    library(tidyr)
    
    df %>%
      pivot_longer(cols = starts_with('Store')) %>%
      mutate(value = paste0(round(value/No_Of_Visits * 100, 2), "%")) %>%
      pivot_wider()
    

    数据

    df <- structure(list(VisitID = structure(1:5, .Label = c("A1", "B1", 
    "C1", "D1", "E1"), class = "factor"), No_Of_Visits = c(1L, 2L, 
    4L, 3L, 4L), Store.A = c(1L, 1L, 1L, 2L, 1L), Store.B = c(0L, 
    0L, 2L, 0L, 1L), Store.C = c(0L, 0L, 1L, 1L, 1L), Store.D = c(0L, 
    1L, 0L, 0L, 1L)), class = "data.frame", row.names = c(NA, -5L))
    

    【讨论】:

      猜你喜欢
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多