问题描述
我有一个看起来像这样的数据
I have a data set that looks like this
Id Subject Date Vitals Value
10 John 2001-05-29 HeartRate 65
10 John 2001-05-29 HeartRate 68
10 John 2001-05-29 BP-Arterial 48
10 John 2001-05-29 PulseRate 64
34 Pete 2005-08-15 HeartRate 68
34 Pete 2005-08-15 BP-Arterial 56
10 John 2004-09-25 HeartRate 65
10 John 2004-09-25 BP-Arterial 64
10 John 2004-09-25 PulseRate 63
34 Pete 2007-07-21 BP-Arterial 68
34 Pete 2007-07-21 PulseRate 56
我想做两件事,
1)按Vitals分组。
2)计算在特定日期(ID +日期)
为每个ID测得的生命值,然后像下面这样折叠并粘贴这些值。 / p>
1) group by Vitals.2) Count the number of Vitals that were measured for each ID on a specific date (ID + Date)
and collapse and paste these values like this below.
Vitals Series
HeartRate 2,1,1
BP-Arterial 1,1,1,1
PulseRate 1,1,1
HeartRate 是 2、1、1
,因为对HeartRate进行了测量
The value under the Series column for HeartRate
is 2, 1, 1
because HeartRate was measured
两次ID 10 on 2001-05-29,
twice for ID 10 on 2001-05-29,
一次,ID为2005-08-15,并且
once for ID 34 on 2005-08-15 and
一次对于2004年9月24日的ID 10
once for ID 10 on 2004-09-24
不知道如何使用dplyr折叠和粘贴这些值,非常感谢。
Not sure how to collapse and paste these values using dplyr any help is much appreciated.
推荐答案
这意味着您需要对这三个部分进行分组。然后,我们可以仅按生命周期进行最后一次崩溃的重新组合:
This means you need to group by all three. We can then regroup by only vitals for the final collapse:
dat %>% group_by(Vitals, Id, Date) %>%
summarize(n = n()) %>%
ungroup() %>%
group_by(Vitals) %>%
summarize(Series = paste(n, collapse = ','))
# # A tibble: 3 × 2
# Vitals Series
# <fctr> <chr>
# 1 BP-Arterial 1,1,1,1
# 2 HeartRate 2,1,1
# 3 PulseRate 1,1,1
这篇关于r dplyr group_by值折叠并粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!