【问题标题】:How to reshape data frame from a row level to person level in R如何在R中将数据框从行级别重塑为人员级别
【发布时间】:2020-03-14 23:34:57
【问题描述】:

我有以下 Netflix 实验代码,以降低 Netflix 的价格,看看人们看电视是多还是少。每次有人使用 Netflix 时,它都会显示他们观看的内容以及观看时长。

**library(tidyverse)
sample_size <- 10000
set.seed(853)
viewing_data <-
tibble(unique_person_id = sample(x = c(1:100),
size = sample_size,
replace = TRUE),
tv_show = sample(x = c("Broadchurch", "Duty-Shame", "Drive to Survive", "Shetland", "The Crown"),
size = sample_size,
replace = TRUE),
)**

然后我想编写一些代码,将人们随机分配到两组中的一组 - 治疗组和控制组。但是,数据集位于行级别,因为有 1000 个观察值。我想将其更改为 R 中的人员级别,然后我可以签署一个人是否接受治疗。一个人不应该既被治疗又不被治疗。但是,tv_show 为一个人播放了很多次。有谁知道在这种情况下如何重塑数据集?

【问题讨论】:

  • 您希望处理和未处理的数量相等吗?

标签: r dplyr reshape


【解决方案1】:
library(dplyr)
treatment <- viewing_data %>% 
  distinct(unique_person_id) %>% 
  mutate(treated = sample(c("yes", "no"), size = 100, replace = TRUE))

viewing_data %>% 
  left_join(treatment, by = "unique_person_id")

如果需要,您可以更改采样方式...

【讨论】:

    【解决方案2】:

    您可以执行以下操作,这会按人员 ID 对您的观察结果进行分组,并为每组分配一个独特的“治疗/控制”:

    library(dplyr)
    viewing_data %>% 
    group_by(unique_person_id) %>% 
    mutate(group=sample(c("treated","control"),1))
    
    # A tibble: 10,000 x 3
    # Groups:   unique_person_id [100]
       unique_person_id tv_show          group  
                  <int> <chr>            <chr>  
     1                9 Drive to Survive control
     2               64 Shetland         treated
     3               90 The Crown        treated
     4               93 Drive to Survive treated
     5               17 Duty-Shame       treated
     6               29 The Crown        control
     7               84 Broadchurch      control
     8               83 The Crown        treated
     9                3 The Crown        control
    10               33 Broadchurch      control
    # … with 9,990 more rows
    

    我们可以检查我们的结果,所有的 id 都只有 1 组处理/控制:

    newdata <- viewing_data %>% 
        group_by(unique_person_id) %>% 
        mutate(group=sample(c("treated","control"),1))
    
    tapply(newdata$group,newdata$unique_person_id,n_distinct)
      1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
      1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
     21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
      1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
     41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
      1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
     61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
      1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
     81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
      1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
    

    【讨论】:

      【解决方案3】:

      如果您希望将人员随机且平等分配到两组(完全随机分配),您可以使用以下代码。

      library(dplyr)
      
      Persons <- viewing_data %>%
        distinct(unique_person_id) %>%
        mutate(group=sample(100),  # in case the ids are not truly random
               group=ifelse(group %% 2 == 0, 0, 1))  # works if only two groups
      Persons
      

      # A tibble: 100 x 2
         unique_person_id group
                    <int> <dbl>
       1                1     0
       2                2     0
       3                3     1
       4                4     0
       5                5     1
       6                6     1
       7                7     1
       8                8     0
       9                9     1
      10               10     0
      # ... with 90 more rows
      

      检查我们每组有 50 个:

      Persons %>% count(group)
      

      # A tibble: 2 x 2
        group     n
        <dbl> <int>
      1     0    50
      2     1    50
      

      您还可以使用 randomizr 包,它除了完全随机分配之外还有更多功能。

      library(randomizr)
      
      Persons <- viewing_data %>%
        distinct(unique_person_id) %>%
        mutate(group=complete_ra(N=100, m=50))
      
      Persons %>% count(group) # Check
      

      要将其链接回查看数据,请使用inner_join

      viewing_data %>% inner_join(Persons, by="unique_person_id")
      
      # A tibble: 10,000 x 3
         unique_person_id tv_show          group
                    <int> <chr>            <int>
       1               10 Shetland             1
       2               95 Broadchurch          0
       3                7 Duty-Shame           1
       4               68 Drive to Survive     0
       5               17 Drive to Survive     1
       6               70 Shetland             0
       7               78 Drive to Survive     0
       8               21 Broadchurch          1
       9               80 The Crown            0
      10               70 Shetland             0
      # ... with 9,990 more rows
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        • 2017-10-20
        • 1970-01-01
        • 2019-10-08
        • 1970-01-01
        • 2021-04-02
        相关资源
        最近更新 更多