我正在尝试使用包中的pivot_longer
函数将dplyr
我的数据转换为长格式。当前的广泛数据涉及对患者年龄、收缩压以及是否使用降压药物 (med_hypt) 以及时间不变的“性别”变量的 3 次重复测量。
示例数据和我尝试过的内容:
library(tidyverse)
library(dplyr)
library(magrittr)
wide_data <- structure(list(id = c(12002, 17001, 17002, 42001, 66001, 82002, 166002, 177001, 177002, 240001),
sex = structure(c(2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L),
.Label = c("men", "women"), class = "factor"),
time1_age = c(71.2, 67.9, 66.5, 57.7, 57.1, 60.9, 80.9, 59.7, 58.2, 66.6),
time1_systolicBP = c(102, 152, NA_real_, 170, 151, 135, 162, 133, 131, 117),
time1_med_hypt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
time2_age = c(74.2, 69.2, 67.8, 58.9, 58.4, 62.5, 82.2, 61, 59.5, 67.8),
time2_systolicBP = c(NA_real_, 146, NA_real_, 151, 129, 129, 137, 144, NA_real_, 132),
time2_med_hypt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
time3_age = c(78, 74.2, 72.8, 64.1, 63.3, 67.7, 87.1, 66, 64.5, 72.9),
time3_systolicBP = c(NA_real_, 160.5, NA_real_, 171, 135, 160, 151, 166, 129, 150.5),
time3_med_hypt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)),
row.names = c(NA, 10L), class = "data.frame")
# Pivoting to a longer format
long_data <- wide_data %>%
pivot_longer(
cols=!id,
names_to=c(".value", "time"),
names_sep="_",
values_drop_na=FALSE
)
这会产生以下小标题:
# A tibble: 40 x 6
id time sex time1 time2 time3
<dbl> <chr> <fct> <dbl> <dbl> <dbl>
1 12002 NA women NA NA NA
2 12002 age NA 71.2 74.2 78
3 12002 systolicBP NA 102 NA NA
4 12002 med NA 0 0 0
5 17001 NA men NA NA NA
6 17001 age NA 67.9 69.2 74.2
7 17001 systolicBP NA 152 146 160.
8 17001 med NA 0 0 0
9 17002 NA women NA NA NA
10 17002 age NA 66.5 67.8 72.8
# ... with 30 more rows
我想要的是列名是 id、time、age、sex、systolicBP 和 med_hypt。每个患者 3 行对应于 3 次重复测量。
有什么帮助吗?