使用 rhandsontable 转置计数

Transpose the with count using rhandsontable

有了下面这个table,我们可以用rhandsontable转置吗?因此,如果用户居住在某个地方,则为是,否则为否

df
User Places
A    fsdfsd
B    fsdfsd
C    fsdfsd
A    fsfgfd
B    fsfgfd

预期产出

User    fsdfsd  fsfgfd  Total Yes
A        Yes    Yes        2
B        Yes    Yes        2
C        Yes    No         1

使用 dplyrtidyr 包的解决方案

library(dplyr)
library(tidyr)

df %>% 
  mutate(value = "Yes") %>% 
  pivot_wider(User, names_from = Places, values_from = value, values_fill = "No") %>% 
  rowwise() %>% 
  mutate(`Total Yes` = sum(c_across(-User) == "Yes"))

输出

# A tibble: 3 x 4
# Rowwise: 
#   User  fsdfsd fsfgfd `Total Yes`
#   <chr> <chr>  <chr>        <int>
# 1 A     Yes    Yes              2
# 2 B     Yes    Yes              2
# 3 C     Yes    No               1