我有一个1995年至2014年运行的透析数据集。它具有变量“ id”,“ name”,“ date”和“ modality”
我对“高清”模式感兴趣。
数据帧遵循以下结构: -从1995年4月开始(然后逐月列出,直到2014年12月) -可以在多个月内找到个人(例如,Name1可以在1995年4月至1997年3月之间接受透析;因此为什么要列出多次) -每行都有日期的是一个会话(我需要确定每位患者每周的会话频率)。
希望以上内容对我要做的事情有意义。
以下是数据集的示例:
id name date modality
10101650 name1 03-Apr-95 HD
10101650 name1 05-Apr-95 HD
10101650 name1 07-Apr-95 HD
10101650 name1 10-Apr-95 HD
10101650 name1 12-Apr-95 HD
10101650 name1 14-Apr-95 HD
10101650 name1 17-Apr-95 HD
10101650 name1 19-Apr-95 HD
10101650 name1 21-Apr-95 HD
10101650 name1 22-Apr-95 HD
10101650 name1 24-Apr-95 HD
10101650 name1 26-Apr-95 HD
10101650 name1 28-Apr-95 HD
10206042 name2 03-Apr-95 HD
10206042 name2 05-Apr-95 HD
10206042 name2 07-Apr-95 HD
10206042 name2 10-Apr-95 HD
10206042 name2 12-Apr-95 HD
10206042 name2 14-Apr-95 HD
10206042 name2 17-Apr-95 HD
10206042 name2 19-Apr-95 HD
10206042 name2 21-Apr-95 HD
10206042 name2 24-Apr-95 HD
10206042 name2 26-Apr-95 HD
10206042 name2 28-Apr-95 HD
10101650 name1 01-May-95 HD
10101650 name1 03-May-95 HD
10101650 name1 05-May-95 HD
10101650 name1 08-May-95 HD
10101650 name1 10-May-95 HD
10101650 name1 12-May-95 HD
10101650 name1 15-May-95 HD
10101650 name1 17-May-95 HD
10101650 name1 19-May-95 HD
10101650 name1 22-May-95 HD
10101650 name1 24-May-95 HD
10101650 name1 26-May-95 HD
10101650 name1 29-May-95 HD
10101650 name1 31-May-95 HD
10205987 name3 01-May-95 HD
10205987 name3 03-May-95 HD
10205987 name3 05-May-95 HD
10205987 name3 08-May-95 HD
10205987 name3 10-May-95 HD
10205987 name3 12-May-95 HD
10205987 name3 15-May-95 HD
10205987 name3 17-May-95 HD
10205987 name3 19-May-95 HD
10205987 name3 22-May-95 HD
10205987 name3 24-May-95 HD
10205987 name3 26-May-95 HD
10205987 name3 29-May-95 HD
10205987 name3 31-May-95 HD
10206042 name2 01-May-95 HD
10206042 name2 03-May-95 HD
10206042 name2 05-May-95 HD
10206042 name2 08-May-95 HD
10206042 name2 10-May-95 HD
10206042 name2 12-May-95 HD
10206042 name2 15-May-95 HD
10206042 name2 17-May-95 HD
10206042 name2 19-May-95 HD
10206042 name2 22-May-95 HD
10206042 name2 24-May-95 HD
10206042 name2 26-May-95 HD
如上所述,我需要每位患者每周接受多少次治疗。这将是一个平均值,因为患者可以接受几年的透析。
答案 0 :(得分:1)
以下是使用dplyr
和lubridate
软件包的方法-
library(dplyr)
library(lubridate)
df$week_year <- paste(week(df$date), year(df$date), sep = "-")
filter(df, modality == "HD") %>%
group_by(id, name, week_year) %>%
summarise(sessions = n()) %>%
group_by(id, name) %>%
summarize(avg_sessions_per_week = mean(sessions))
# A tibble: 3 x 3
# Groups: id [?]
# id name avg_sessions_per_week
# <int> <chr> <dbl>
# 1 10101650 name1 3.00
# 2 10205987 name3 2.80
# 3 10206042 name2 3.00
数据-
df <- structure(list(id = c(10101650L, 10101650L, 10101650L, 10101650L,
10101650L, 10101650L, 10101650L, 10101650L, 10101650L, 10101650L,
10101650L, 10101650L, 10101650L, 10206042L, 10206042L, 10206042L,
10206042L, 10206042L, 10206042L, 10206042L, 10206042L, 10206042L,
10206042L, 10206042L, 10206042L, 10101650L, 10101650L, 10101650L,
10101650L, 10101650L, 10101650L, 10101650L, 10101650L, 10101650L,
10101650L, 10101650L, 10101650L, 10101650L, 10101650L, 10205987L,
10205987L, 10205987L, 10205987L, 10205987L, 10205987L, 10205987L,
10205987L, 10205987L, 10205987L, 10205987L, 10205987L, 10205987L,
10205987L, 10206042L, 10206042L, 10206042L, 10206042L, 10206042L,
10206042L, 10206042L, 10206042L, 10206042L, 10206042L, 10206042L,
10206042L), name = c("name1", "name1", "name1", "name1", "name1",
"name1", "name1", "name1", "name1", "name1", "name1", "name1",
"name1", "name2", "name2", "name2", "name2", "name2", "name2",
"name2", "name2", "name2", "name2", "name2", "name2", "name1",
"name1", "name1", "name1", "name1", "name1", "name1", "name1",
"name1", "name1", "name1", "name1", "name1", "name1", "name3",
"name3", "name3", "name3", "name3", "name3", "name3", "name3",
"name3", "name3", "name3", "name3", "name3", "name3", "name2",
"name2", "name2", "name2", "name2", "name2", "name2", "name2",
"name2", "name2", "name2", "name2"), date = structure(c(9223,
9225, 9227, 9230, 9232, 9234, 9237, 9239, 9241, 9242, 9244, 9246,
9248, 9223, 9225, 9227, 9230, 9232, 9234, 9237, 9239, 9241, 9244,
9246, 9248, 9251, 9253, 9255, 9258, 9260, 9262, 9265, 9267, 9269,
9272, 9274, 9276, 9279, 9281, 9251, 9253, 9255, 9258, 9260, 9262,
9265, 9267, 9269, 9272, 9274, 9276, 9279, 9281, 9251, 9253, 9255,
9258, 9260, 9262, 9265, 9267, 9269, 9272, 9274, 9276), class = "Date"),
modality = c("HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD",
"HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD",
"HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD",
"HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD",
"HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD",
"HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD",
"HD", "HD", "HD", "HD", "HD", "HD", "HD")), .Names = c("id",
"name", "date", "modality"), row.names = c(NA, -65L), class = "data.frame")