具有相同患者ID的多个条目:如何使其成为一个条目

时间:2019-01-30 05:53:12

标签: r

我的数据中有来自同一患者ID的多个条目,我想将其作为一个条目。我可能有哪些选择?这是数据-

PtID    WorryHighBGNow
40  5
40  1
40  2
70  3
101 4
263 2
263 5
263 3
143 4
245 4
137 3
219 2
219 3
219 4
3   3
264 3
264 3
98  1
200 3
105 3
111 4
149 3

我想根据这些数据创建如下所示的可视化效果,在y轴上我想查看表的列,而在X轴上我想确定1,2,3,4,5的排名

picture of visualization

2 个答案:

答案 0 :(得分:0)

如果x是您的数据框,您可以尝试

                      d <- setDT(x)[, list(WorryHighBGNow = paste(WorryHighBGNow, collapse = ', ')),by = c('PtID')] 

它将给出类似

的结果
                     PtID   WorryHighBGNow
                     40     5,1,2
                     70     3
                     101    4
                     263    2,5,3

以此类推。

答案 1 :(得分:0)

不是很确定那是您所需要的。我只是想尽可能地模仿visualization you linked to the question

library(tidyverse)

dat %>%
  mutate_all(factor) %>%
  count(WorryHighBGNow, ) %>%
  mutate(percentage = round(n / sum(n) * 100, 1)) %>%
  mutate(WorryHighBGNow =  reorder(WorryHighBGNow, n)) %>%
  ggplot(aes(x    = WorryHighBGNow, y     = percentage, 
             fill = WorryHighBGNow, label = paste(percentage, '%'))) +
  geom_col() +
  geom_text(hjust = -.1, fontface = 'bold') +
  scale_fill_brewer(type = 'qual', breaks = 1:5) +
  coord_flip() +
  expand_limits(y = 50) +
  theme_void() +
  theme(legend.position = 'bottom')

scores baplot

数据

dat <- structure(
  list(
    PtID = c(40L, 40L, 40L, 70L, 101L, 263L, 263L, 263L, 143L, 245L, 137L, 219L, 
             219L, 219L, 3L, 264L, 264L, 98L, 200L, 105L, 111L, 149L), 
    WorryHighBGNow = c(5L, 1L, 2L, 3L, 4L, 2L, 5L, 3L, 4L, 4L, 3L, 2L, 3L, 4L, 
                       3L, 3L, 3L, 1L, 3L, 3L, 4L, 3L)
    ), 
  class = "data.frame", row.names = c(NA, -22L)
  )