Один dplyr
и tidyr
параметр может быть:
df1 %>%
pivot_longer(everything(), names_to = "Column", values_to = "Name") %>%
group_by(Name, Column) %>%
slice(1) %>%
group_by(Name) %>%
summarise(Column = toString(Column),
Time = n())
Name Column Time
<fct> <chr> <int>
1 Emily,Cute A1, A2, A4 3
2 John,Price A1, A2 2
3 Rose,Sarah A1, A2 2
4 Hana,Pipe A3 1
5 Smith,Roger A3 1
6 Dave,Yellow A4 1
Если вы хотите получить точные результаты, также с итогами:
df1 %>%
pivot_longer(everything(), names_to = "Column", values_to = "Name") %>%
group_by(Name, Column) %>%
slice(1) %>%
group_by(Name) %>%
summarise(Column = toString(Column),
Time = n()) %>%
ungroup() %>%
mutate_if(is.factor, as.character) %>%
add_row() %>%
mutate(Name = ifelse(row_number() == max(row_number()), n_distinct(Name)-1, Name),
Column = ifelse(row_number() == max(row_number()), "Total", Column),
Time = ifelse(row_number() == max(row_number()), sum(Time, na.rm = TRUE), Time)) %>%
select(Column, Name, Time)
Column Name Time
<chr> <chr> <int>
1 A1, A2, A4 Emily,Cute 3
2 A1, A2 John,Price 2
3 A1, A2 Rose,Sarah 2
4 A3 Hana,Pipe 1
5 A3 Smith,Roger 1
6 A4 Dave,Yellow 1
7 Total 6 10