Группировать по столбцу строк, разделенных запятыми, но группировка должна игнорировать определенный порядок строк - PullRequest
0 голосов
/ 05 марта 2019

Допустим, у меня есть следующие данные

> summary_table[, c('condition_list', 'condition_count')]
# A tibble: 4,306 x 2
   condition_list             condition_count
   <chr>                                <int>
 1 true control,control email               2
 2 true control,control email               1
 3 treatment, control email                 1
 4 true control, control email              1
 5 control email, true control              1
 6 control email                            1
 7 control email, treatment                 1
 8 control email,true control               2
 9 treatment                                1
10 control email, true control              1

Обратите внимание, что столбец "condition_list" состоит из строк с ограничением запятыми, указывающих назначение некоторому условию, но некоторые из этих назначений изоморфны друг другу.Я хотел бы подсчитать количество строк в каждом условии следующим образом:

summary_table %>% group_by(condition_list) %>%
  summarize(n= n())

Однако это будет рассматривать каждую конкретную комбинацию condition_list как отдельную группу.Я хочу, чтобы это относилось к «контролю электронной почты, истинному контролю» так же, как «истинному контролю, контролю электронной почты».Каков наилучший способ сделать это?

> dput(dputter)
structure(list(condition_list = c("true control,control email", 
"true control", "treatment", "true control", "control email", 
"control email", "control email", "control email,true control", 
"treatment", "control email", "true control,treatment", "treatment,true control", 
"treatment,true control,control email", "control email", "treatment", 
"true control,control email", "control email", "treatment", "true control,treatment", 
"control email", "control email,true control", "treatment", "control email", 
"control email", "control email,true control", "control email", 
"control email", "true control", "treatment", "true control", 
"treatment", "true control", "true control", "control email", 
"true control", "control email", "control email", "true control", 
"treatment", "treatment,true control,control email", "true control", 
"true control", "treatment,control email", "true control", "true control", 
"control email", "control email", "treatment", "control email", 
"true control"), condition_count = c(2L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 2L, 2L, 3L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -50L))

Ответы [ 2 ]

0 голосов
/ 05 марта 2019

Вот решение по тидиверу:

library(tidyverse)

summary_table %>% 
  mutate(condition_list = 
           strsplit(condition_list, ",") %>% 
           map(sort) %>% 
           map_chr(paste, collapse = ",")
         ) %>%
  group_by(condition_list) %>% 
  tally()
# A tibble: 7 x 2
#  condition_list                           n
#  <chr>                                <int>
#1 control email                           17
#2 control email,treatment                  1
#3 control email,treatment,true control     2
#4 control email,true control               5
#5 treatment                                9
#6 treatment,true control                   3
#7 true control                            13
0 голосов
/ 05 марта 2019

Вы имеете в виду что-то вроде этого?

dputter %>%
    mutate(condition_list = str_split(condition_list, ",")) %>%
    unnest() %>%
    group_by(condition_list) %>%
    tally()
## A tibble: 3 x 2
#  condition_list     n
#  <chr>          <int>
#1 control email     25
#2 treatment         15
#3 true control      23

Объяснение: Вместо separate мы можем использовать str_split (или в базе R strsplit) для разделения записей на ",", получаяlist столбец, который мы затем unnest перед суммированием.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...