如何在 gtsummary 中对每个变量使用 complete.cases 来进行配对 t.test 而不是对完整数据帧进行 complete.cases ?
How to use complete.cases in gtsummary for each variable for doing a paired t.test instead doing complete.cases for full data frame?
我正在尝试对我的数据进行配对 t.test
以进行 post 前分析,并使用 gtsummary
包来创建 table。因为我缺少数据,所以我 filter
数据框 complete.cases(.)
但是当它过滤所有列时,我丢失了很多数据。取而代之的是,我只希望过滤器 complete.cases()
只针对它每次测试的特定变量。例如:如果它正在对 variable1
进行测试,它应该只检查 complete.cases()
的 variable1
。有人可以帮我完成吗?以下是我现在使用的代码。
trial_paired <-
df %>% filter(OSAclass == 'OSA') %>% select(c('time1', 'CPAP','Cholesterol', 'Triglyceride','HDL_chol','LDL_chol'))%>%
group_by(time1) %>%
mutate(id = row_number()) %>%
ungroup()
t2 <-
trial_paired %>%
# delete missing values
filter(complete.cases(.)) %>%
# keep IDs with both measurements
group_by(id) %>%
filter(n() == 2) %>%
ungroup() %>%
# summarize data
tbl_summary(by = time1 , include = -id, type = all_continuous() ~ "continuous2", statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min}, {max}", "{mean} ({sd})")) %>%
add_p(test = list(all_continuous() ~ "paired.t.test",
all_categorical() ~ "mcnemar.test"),
group = id)
structure(list(time1 = c("first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second"), CPAP = c(1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
1, 0, 1, 1, 1, 1, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0), Cholesterol = c(4.83, 4.83, 4.81, 4.81, 4.48, 4.48,
4.25, 4.25, 4.93, 4.93, 5.57, 5.57, 5.52, 5.52, 5.47, 5.47, 4.61,
4.61, 5.4, 5.4, 5.31, 5.31, 4.89, 4.89, 6.62, 6.62, 5.15, 5.15,
4.7, 4.7, 4.62, 4.62, 4.66, 4.66, 5.17, 5.17, 4.78, 4.78, 8.82,
8.82, 4.28, 4.28, 4.9, 4.9, 2.9, 2.9, 5.92, 5.92, 5.39, 5.39,
4.92, 4.92, 3.75, 3.75, 3.87, 3.87, 6.1, 6.1, 6.05, 6.05, 5.18,
5.18, 4.57, 4.57, 5.42, 5.42, 6.08, 6.08, 5.48, 5.48, 4.78, 4.78,
3.89, 3.89, 4.62, 4.62, 4.6, 4.6, 6.02, 6.02, 3.67, 3.67, 6.06,
6.06, 6.12, 6.12, 4.84, 4.84, 5.86, 5.86, 5.9, 5.9, 6.27, 6.27,
3.87, 3.87, 7.4, 7.4, 5.55, 5.55, 4.45, 4.45, 5.26, 5.26, 4.62,
4.62, 7.17, 7.17, 5.35, 5.35, 5.99, 5.99, 5.94, 5.94, 4.38, 4.38,
5.2, 5.2, 4.68, 4.68, 3.29, 3.29, 4.85, 4.85, 4.83, 4.83, 5.21,
5.21, 6.61, 6.61, 6.33, 6.33, 5.59, 5.59, 7.14, 7.14, 4.8, 4.8,
4.22, 4.22, 5.45, 5.45, 4.87, 4.87, 5.89, 5.89, 5.1, 5.1, 4.18,
4.18, 5.58, 5.58, 6.41, 6.41, 4.26, 4.26, 4.88, 4.88, 4.3, 4.3,
6.51, 6.51, 5.19, 5.19, 6, 6, 4.39, 4.39, 6, 6, 4.73, 4.73, 6.23,
6.23, 4.51, 4.51), Triglyceride = c(4.62, 4.62, 1.16, 1.16, 2.29,
2.29, 2.41, 2.41, 2.88, 2.88, 2.89, 2.89, 5.22, 5.22, 2.3, 2.3,
0.95, 0.95, 2.21, 2.21, 2.54, 2.54, 1.98, 1.98, 3.4, 3.4, 1.77,
1.77, 1.95, 1.95, 3.53, 3.53, 1.17, 1.17, 1.04, 1.04, 2.53, 2.53,
2.69, 2.69, 0.71, 0.71, 1.32, 1.32, 0.82, 0.82, 2.75, 2.75, 1.76,
1.76, 3.59, 3.59, 2.38, 2.38, 1.87, 1.87, 2.06, 2.06, 15.53,
15.53, 1.66, 1.66, 1.57, 1.57, 1.23, 1.23, 1.99, 1.99, 1.98,
1.98, 2, 2, 1.52, 1.52, 0.92, 0.92, 1.49, 1.49, 3.4, 3.4, 1.39,
1.39, 1.06, 1.06, 3.37, 3.37, 0.9, 0.9, 1.49, 1.49, 1.8, 1.8,
1.45, 1.45, 1.44, 1.44, 3.9, 3.9, 0.95, 0.95, 0.89, 0.89, 0.74,
0.74, 2.42, 2.42, 3.99, 3.99, 1.32, 1.32, 2.27, 2.27, 2.09, 2.09,
1.53, 1.53, 2.02, 2.02, 2.38, 2.38, 1.06, 1.06, 1.71, 1.71, 1.16,
1.16, 1.41, 1.41, 2.9, 2.9, 1.17, 1.17, 1.41, 1.41, 2.84, 2.84,
2.94, 2.94, 0.67, 0.67, 1.83, 1.83, 2.33, 2.33, 2.82, 2.82, 1.47,
1.47, 0.82, 0.82, 2.96, 2.96, 2.84, 2.84, 2.04, 2.04, 3.14, 3.14,
1.44, 1.44, 2.14, 2.14, 0.85, 0.85, 2.39, 2.39, 1.1, 1.1, 1.52,
1.52, 1.41, 1.41, 2.64, 2.64, 1.06, 1.06), HDL_chol = c(0.81,
0.81, 0.86, 0.86, 1.3, 1.3, 0.99, 0.99, 1.06, 1.06, 1.31, 1.31,
1.01, 1.01, 1.02, 1.02, 1.38, 1.38, 1.31, 1.31, 1.63, 1.63, 1.63,
1.63, 1.27, 1.27, 1.28, 1.28, 0.99, 0.99, 0.94, 0.94, 1.14, 1.14,
2.14, 2.14, 1.74, 1.74, 1.19, 1.19, 1.03, 1.03, 1.19, 1.19, 1.75,
1.75, 0.93, 0.93, 1.85, 1.85, 0.88, 0.88, 1.02, 1.02, 1.05, 1.05,
1.1, 1.1, 0.38, 0.38, 0.95, 0.95, 1.15, 1.15, 1.38, 1.38, 1.34,
1.34, 0.86, 0.86, 1.02, 1.02, 1.19, 1.19, 1.89, 1.89, 1.22, 1.22,
1.37, 1.37, 0.92, 0.92, 1.33, 1.33, 1.44, 1.44, 1.28, 1.28, 1.28,
1.28, 1.18, 1.18, 1.32, 1.32, 1.98, 1.98, 1.23, 1.23, 1.93, 1.93,
0.76, 0.76, 1.72, 1.72, 1.24, 1.24, 1.13, 1.13, 1.88, 1.88, 1.27,
1.27, 1.34, 1.34, 1.28, 1.28, 0.9, 0.9, 1.07, 1.07, 1.25, 1.25,
1.41, 1.41, 1.59, 1.59, 1.35, 1.35, 1.47, 1.47, 1.41, 1.41, 2.37,
2.37, 1.17, 1.17, 1.35, 1.35, 1.02, 1.02, 1.32, 1.32, 0.86, 0.86,
1.62, 1.62, 1.11, 1.11, 1.17, 1.17, 1, 1, 1.28, 1.28, 1.16, 1.16,
0.93, 0.93, 1.13, 1.13, 1.24, 1.24, 1.76, 1.76, 0.89, 0.89, 1.55,
1.55, 1.76, 1.76, 1.34, 1.34, 1.86, 1.86, 1.29, 1.29), LDL_chol = c(2.49,
2.49, 3.58, 3.58, 2.7, 2.7, 2.42, 2.42, 3.25, 3.25, 3.58, 3.58,
3.15, 3.15, 3.78, 3.78, 3.06, 3.06, 3.56, 3.56, 2.97, 2.97, 2.74,
2.74, 4.72, 4.72, 3.34, 3.34, 3.17, 3.17, 2.87, 2.87, 3.09, 3.09,
2.87, 2.87, 2.56, 2.56, 7.19, 7.19, 2.87, 2.87, 3.28, 3.28, 1.2,
1.2, 4.2, 4.2, 3.22, 3.22, 3.1, 3.1, 2.27, 2.27, 2.43, 2.43,
4.49, 4.49, 1.52, 1.52, 3.67, 3.67, 2.97, 2.97, 3.67, 3.67, 4.3,
4.3, 3.96, 3.96, 3.2, 3.2, 2.41, 2.41, 2.64, 2.64, 3.03, 3.03,
3.82, 3.82, 2.28, 2.28, 4, 4, 3.91, 3.91, 3.27, 3.27, 4.07, 4.07,
4.11, 4.11, 4.47, 4.47, 2.39, 2.39, 5.23, 5.23, 3.43, 3.43, 3.13,
3.13, 3.13, 3.13, 2.55, 2.55, 4.99, 4.99, 3.16, 3.16, 4.05, 4.05,
4.15, 4.15, 2.6, 2.6, 3.54, 3.54, 2.74, 2.74, 1.59, 1.59, 2.79,
2.79, 2.77, 2.77, 3.32, 3.32, 4.3, 4.3, 4.56, 4.56, 2.87, 2.87,
5.29, 5.29, 2.7, 2.7, 2.85, 2.85, 3.55, 3.55, 3.26, 3.26, 3.4,
3.4, 3.49, 3.49, 2.59, 2.59, 3.74, 3.74, 4.24, 4.24, 2.73, 2.73,
2.98, 2.98, 2.87, 2.87, 4.89, 4.89, 3.38, 3.38, 4.35, 4.35, 2.51,
2.51, 4.16, 4.16, 2.99, 2.99, 3.92, 3.92, 2.77, 2.77), ANGPTL8 = c(3337.5,
3962.5, 2737.5, 962.5, 1775, 3737.5, 1025, 962.5, 1175, 912.5,
1662.5, 2075, 2862.5, 1950, 2337.5, 1875, 350, 14412.5, 962.5,
787.5, 1650, 2150, 3250, 1150, 1425, 1162.5, 975, 762.5, 5562.5,
2662.5, 1450, 787.5, 387.5, 475, 1037.5, 1125, 1462.5, 1750,
1137.5, 800, 812.5, 1637.5, 750, 4850, 1112.5, 1187.5, 662.5,
462.5, 4125, 1825, 1275, 750, 6275, 1062.5, 737.5, 3650, 1650,
1425, 2925, 1512.5, 1100, 887.5, 662.5, 825, 487.5, 662.5, 400,
600, 1077.77777777778, 1211.11111111111, 555.555555555556, 511.111111111111,
1066.66666666667, 1311.11111111111, 277.777777777778, 1822.22222222222,
1000, 1055.55555555556, 1255.55555555556, 1000, 1555.55555555556,
1266.66666666667, 1233.33333333333, 1422.22222222222, 1655.55555555556,
800, 555.555555555556, 677.777777777778, 411.111111111111, 344.444444444445,
766.666666666667, 800, 333.333333333333, 1011.11111111111, 455.555555555555,
955.555555555556, 833.333333333333, 777.777777777778, 844.444444444444,
866.666666666667, 755.555555555556, 1011.11111111111, 722.222222222222,
888.888888888889, 255.555555555556, 244.444444444445, 1433.33333333333,
1033.33333333333, 488.888888888889, 477.777777777778, 1600, 1022.22222222222,
1077.77777777778, 988.888888888889, 622.222222222222, 2500, 2077.77777777778,
688.888888888889, 788.888888888889, 1155.55555555556, 1288.88888888889,
1633.33333333333, 1744.44444444445, 2011.11111111111, 366.666666666667,
466.666666666667, 522.222222222222, 1222.22222222222, 477.777777777778,
788.888888888889, 994.444444444445, 1383.33333333333, 2183.33333333333,
661.111111111111, 2350, 1772.22222222222, 672.222222222222, 1183.33333333333,
494.444444444445, 883.333333333333, 416.666666666667, 338.888888888889,
2005.55555555555, 594.444444444444, NA, 305.555555555555, 961.111111111111,
1138.88888888889, 616.666666666667, 583.333333333333, 1405.55555555556,
705.555555555555, 1605.55555555556, 1594.44444444445, 1094.44444444444,
1272.22222222222, 3127.77777777778, 961.111111111111, 750, 661.111111111111,
916.666666666667, 572.222222222222, 1150, 1094.44444444444, 683.333333333333,
827.777777777778, 972.222222222222, 238.888888888889, NA, 327.777777777778,
850, 750, 672.222222222222, 827.777777777778, 983.333333333333,
1038.88888888889), BMP_2 = c(23, 26.92, 25.62, 26.27, 25.62,
26.92, 24.97, 26.92, 25.62, 28.2, NA, 26.92, 22.34, 23, 26.92,
24.32, 24.32, 25.62, 24.32, 25.62, 24.32, 23, 25.62, 28.2, 25.62,
24.32, 23, 26.92, 25.62, 28.2, 24.32, 26.92, 18.95, 23, 23, 25.62,
23, 24.32, 24.32, 23, 25.62, 25.62, 21.67, 26.92, 24.32, 25.62,
21.67, 23, 23, 26.92, 28.2, 24.32, 28.2, 28.2, 26.92, 26.92,
25.62, 25.62, 24.32, 24.32, 24.32, 24.32, 25.62, 23, 17.57, 20.32,
30.61, 27.33, 20.94, 26.16, 23.68, 26.16, 26.16, 28.46, 23.68,
26.16, 20.94, 32.65, 26.16, 28.46, 28.46, 30.61, 26.16, 32.65,
23.68, 28.46, 23.68, 28.46, 19.43, 22.35, 26.16, 28.46, 23.68,
28.46, 26.16, 30.61, 26.16, 28.46, 23.68, 23.68, 28.46, 30.61,
30.61, 30.61, 26.16, 28.46, 20.94, 26.16, 23.68, 30.61, 26.16,
28.46, 20.94, 23.68, 31.64, 26.16, 23.68, 30.61, 23.68, 28.46,
26.16, 30.61, 20.94, 26.16, 14.02, 26.16, 20.94, 23.68, 30.61,
34.58, 23.39, 26.67, 19.74, 19.74, 3, 15.48, 15.48, 23.39, 17.71,
15.48, 15.48, 19.74, 3, 10, NA, 23.39, 19.74, 26.67, 19.74, 19.74,
19.74, 23.39, 17.71, 23.39, 23.39, 26.67, 3, 3, 3, 23.39, 19.74,
19.74, 19.74, 29.69, 33.85, 23.39, 10, 10, 15.48, 23.39, 10,
19.74, 15.48, 15.48, 19.74, 19.74), IGFBP_3_1 = c(441353.12,
NA, 393869.87, NA, NA, NA, 579939.36, NA, 456112.02, NA, NA,
610080.87, NA, NA, 533744.22, 628064.64, 523351.47, NA, 517877.29,
NA, 486315.82, NA, NA, 542659.7, 508437.67, 589967.34, 536282.89,
512564.26, 436271.69, 601179.52, 504448.47, 506264.97, 420330.98,
NA, 538394.66, NA, NA, NA, NA, NA, 495111.88, 549340.97, 672083.18,
NA, 591978.44, NA, NA, 571958.24, 507324.12, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 475288.45, NA, 536037.9, 548109.89,
559995.14, NA, 473616.64, 542571.78, 465343.85, 1127900, 714496.84,
NA, 646959.05, 4856100, 443062.73, 542179.38, 579299.18, 1142900,
564875.53, 1037100, 1174200, NA, 548298.03, 874608.37, 902414.03,
1471500, NA, NA, 1668200, NA, 3153500, 1527000, 534397.71, 556715.71,
1016800, 703025.17, NA, NA, 161911.33, 126486.58, 682462.8, NA,
1365000, NA, 977538.37, NA, 3348600, NA, 1022700, 783787.11,
NA, NA, 859094.87, NA, 1056900, 953743.93, 363547.86, 422392.66,
796697.33, 804929.76, 686250.79, 859712.77, 726741.92, 2091000,
568594.78, 644119.63, 1139000, NA, 802047.77, NA, 1256800, 1442100,
1058500, 974033.9, 967920.77, 981304.96, 1107000, 1197400, 1019800,
1346600, 1135800, 1261900, 1203600, 1352600, NA, 1335400, 1100400,
1398300, 924378.25, 1194500, 1384400, 1186500, 1360700, 1222800,
843925.82, 1232900, 1600800, 1489200, 1133700, 1451700, 1182700,
1445100, 1732100, 1528500, 1321900, 1313500, 1101500, 1422500,
1344700, 1460200, 1224900, 1225100, 1167800, 1155800, 1149200,
1278700)), row.names = c(NA, -176L), class = c("tbl_df", "tbl",
"data.frame"))
您可以使用 !is.na(variable)
仅针对特定变量删除具有 NA
值的行。
library(dplyr)
library(gtsummary)
t2 <-
trial_paired %>%
# delete missing values in variable1
filter(!is.na(variable1)) %>%
# keep IDs with both measurements
group_by(id) %>%
filter(n() == 2) %>%
ungroup() %>%
# summarize data
tbl_summary(by = time1 , include = -id, type = all_continuous() ~ "continuous2", statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min}, {max}", "{mean} ({sd})")) %>%
add_p(test = list(all_continuous() ~ "paired.t.test",
all_categorical() ~ "mcnemar.test"),
group = id)
要动态执行此操作,我们可以创建一个函数。
summary_data <- function(data, var) {
data %>%
# delete missing values
filter(!is.na(.data[[var]])) %>%
# keep IDs with both measurements
group_by(id) %>%
filter(n() == 2) %>%
ungroup() %>%
# summarize data
tbl_summary(by = time1 , include = -id, type = all_continuous() ~ "continuous2", statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min}, {max}", "{mean} ({sd})")) %>%
add_p(test = list(all_continuous() ~ "paired.t.test",
all_categorical() ~ "mcnemar.test"),
group = id)
}
#apply function to single column
summary_data(trial_paired, 'Cholesterol')
summary_data(trial_paired, 'Triglyceride')
#apply function to multiple column
cols <- c('Cholesterol', 'Triglyceride', 'HDL_chol')
#Or drop only the first column
#cols <- names(trial_paired)[-1]
res <- lapply(cols, summary_data, data = trial_paired)
我正在尝试对我的数据进行配对 t.test
以进行 post 前分析,并使用 gtsummary
包来创建 table。因为我缺少数据,所以我 filter
数据框 complete.cases(.)
但是当它过滤所有列时,我丢失了很多数据。取而代之的是,我只希望过滤器 complete.cases()
只针对它每次测试的特定变量。例如:如果它正在对 variable1
进行测试,它应该只检查 complete.cases()
的 variable1
。有人可以帮我完成吗?以下是我现在使用的代码。
trial_paired <-
df %>% filter(OSAclass == 'OSA') %>% select(c('time1', 'CPAP','Cholesterol', 'Triglyceride','HDL_chol','LDL_chol'))%>%
group_by(time1) %>%
mutate(id = row_number()) %>%
ungroup()
t2 <-
trial_paired %>%
# delete missing values
filter(complete.cases(.)) %>%
# keep IDs with both measurements
group_by(id) %>%
filter(n() == 2) %>%
ungroup() %>%
# summarize data
tbl_summary(by = time1 , include = -id, type = all_continuous() ~ "continuous2", statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min}, {max}", "{mean} ({sd})")) %>%
add_p(test = list(all_continuous() ~ "paired.t.test",
all_categorical() ~ "mcnemar.test"),
group = id)
structure(list(time1 = c("first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second", "first", "second", "first",
"second", "first", "second", "first", "second", "first", "second",
"first", "second", "first", "second"), CPAP = c(1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
1, 0, 1, 1, 1, 1, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0), Cholesterol = c(4.83, 4.83, 4.81, 4.81, 4.48, 4.48,
4.25, 4.25, 4.93, 4.93, 5.57, 5.57, 5.52, 5.52, 5.47, 5.47, 4.61,
4.61, 5.4, 5.4, 5.31, 5.31, 4.89, 4.89, 6.62, 6.62, 5.15, 5.15,
4.7, 4.7, 4.62, 4.62, 4.66, 4.66, 5.17, 5.17, 4.78, 4.78, 8.82,
8.82, 4.28, 4.28, 4.9, 4.9, 2.9, 2.9, 5.92, 5.92, 5.39, 5.39,
4.92, 4.92, 3.75, 3.75, 3.87, 3.87, 6.1, 6.1, 6.05, 6.05, 5.18,
5.18, 4.57, 4.57, 5.42, 5.42, 6.08, 6.08, 5.48, 5.48, 4.78, 4.78,
3.89, 3.89, 4.62, 4.62, 4.6, 4.6, 6.02, 6.02, 3.67, 3.67, 6.06,
6.06, 6.12, 6.12, 4.84, 4.84, 5.86, 5.86, 5.9, 5.9, 6.27, 6.27,
3.87, 3.87, 7.4, 7.4, 5.55, 5.55, 4.45, 4.45, 5.26, 5.26, 4.62,
4.62, 7.17, 7.17, 5.35, 5.35, 5.99, 5.99, 5.94, 5.94, 4.38, 4.38,
5.2, 5.2, 4.68, 4.68, 3.29, 3.29, 4.85, 4.85, 4.83, 4.83, 5.21,
5.21, 6.61, 6.61, 6.33, 6.33, 5.59, 5.59, 7.14, 7.14, 4.8, 4.8,
4.22, 4.22, 5.45, 5.45, 4.87, 4.87, 5.89, 5.89, 5.1, 5.1, 4.18,
4.18, 5.58, 5.58, 6.41, 6.41, 4.26, 4.26, 4.88, 4.88, 4.3, 4.3,
6.51, 6.51, 5.19, 5.19, 6, 6, 4.39, 4.39, 6, 6, 4.73, 4.73, 6.23,
6.23, 4.51, 4.51), Triglyceride = c(4.62, 4.62, 1.16, 1.16, 2.29,
2.29, 2.41, 2.41, 2.88, 2.88, 2.89, 2.89, 5.22, 5.22, 2.3, 2.3,
0.95, 0.95, 2.21, 2.21, 2.54, 2.54, 1.98, 1.98, 3.4, 3.4, 1.77,
1.77, 1.95, 1.95, 3.53, 3.53, 1.17, 1.17, 1.04, 1.04, 2.53, 2.53,
2.69, 2.69, 0.71, 0.71, 1.32, 1.32, 0.82, 0.82, 2.75, 2.75, 1.76,
1.76, 3.59, 3.59, 2.38, 2.38, 1.87, 1.87, 2.06, 2.06, 15.53,
15.53, 1.66, 1.66, 1.57, 1.57, 1.23, 1.23, 1.99, 1.99, 1.98,
1.98, 2, 2, 1.52, 1.52, 0.92, 0.92, 1.49, 1.49, 3.4, 3.4, 1.39,
1.39, 1.06, 1.06, 3.37, 3.37, 0.9, 0.9, 1.49, 1.49, 1.8, 1.8,
1.45, 1.45, 1.44, 1.44, 3.9, 3.9, 0.95, 0.95, 0.89, 0.89, 0.74,
0.74, 2.42, 2.42, 3.99, 3.99, 1.32, 1.32, 2.27, 2.27, 2.09, 2.09,
1.53, 1.53, 2.02, 2.02, 2.38, 2.38, 1.06, 1.06, 1.71, 1.71, 1.16,
1.16, 1.41, 1.41, 2.9, 2.9, 1.17, 1.17, 1.41, 1.41, 2.84, 2.84,
2.94, 2.94, 0.67, 0.67, 1.83, 1.83, 2.33, 2.33, 2.82, 2.82, 1.47,
1.47, 0.82, 0.82, 2.96, 2.96, 2.84, 2.84, 2.04, 2.04, 3.14, 3.14,
1.44, 1.44, 2.14, 2.14, 0.85, 0.85, 2.39, 2.39, 1.1, 1.1, 1.52,
1.52, 1.41, 1.41, 2.64, 2.64, 1.06, 1.06), HDL_chol = c(0.81,
0.81, 0.86, 0.86, 1.3, 1.3, 0.99, 0.99, 1.06, 1.06, 1.31, 1.31,
1.01, 1.01, 1.02, 1.02, 1.38, 1.38, 1.31, 1.31, 1.63, 1.63, 1.63,
1.63, 1.27, 1.27, 1.28, 1.28, 0.99, 0.99, 0.94, 0.94, 1.14, 1.14,
2.14, 2.14, 1.74, 1.74, 1.19, 1.19, 1.03, 1.03, 1.19, 1.19, 1.75,
1.75, 0.93, 0.93, 1.85, 1.85, 0.88, 0.88, 1.02, 1.02, 1.05, 1.05,
1.1, 1.1, 0.38, 0.38, 0.95, 0.95, 1.15, 1.15, 1.38, 1.38, 1.34,
1.34, 0.86, 0.86, 1.02, 1.02, 1.19, 1.19, 1.89, 1.89, 1.22, 1.22,
1.37, 1.37, 0.92, 0.92, 1.33, 1.33, 1.44, 1.44, 1.28, 1.28, 1.28,
1.28, 1.18, 1.18, 1.32, 1.32, 1.98, 1.98, 1.23, 1.23, 1.93, 1.93,
0.76, 0.76, 1.72, 1.72, 1.24, 1.24, 1.13, 1.13, 1.88, 1.88, 1.27,
1.27, 1.34, 1.34, 1.28, 1.28, 0.9, 0.9, 1.07, 1.07, 1.25, 1.25,
1.41, 1.41, 1.59, 1.59, 1.35, 1.35, 1.47, 1.47, 1.41, 1.41, 2.37,
2.37, 1.17, 1.17, 1.35, 1.35, 1.02, 1.02, 1.32, 1.32, 0.86, 0.86,
1.62, 1.62, 1.11, 1.11, 1.17, 1.17, 1, 1, 1.28, 1.28, 1.16, 1.16,
0.93, 0.93, 1.13, 1.13, 1.24, 1.24, 1.76, 1.76, 0.89, 0.89, 1.55,
1.55, 1.76, 1.76, 1.34, 1.34, 1.86, 1.86, 1.29, 1.29), LDL_chol = c(2.49,
2.49, 3.58, 3.58, 2.7, 2.7, 2.42, 2.42, 3.25, 3.25, 3.58, 3.58,
3.15, 3.15, 3.78, 3.78, 3.06, 3.06, 3.56, 3.56, 2.97, 2.97, 2.74,
2.74, 4.72, 4.72, 3.34, 3.34, 3.17, 3.17, 2.87, 2.87, 3.09, 3.09,
2.87, 2.87, 2.56, 2.56, 7.19, 7.19, 2.87, 2.87, 3.28, 3.28, 1.2,
1.2, 4.2, 4.2, 3.22, 3.22, 3.1, 3.1, 2.27, 2.27, 2.43, 2.43,
4.49, 4.49, 1.52, 1.52, 3.67, 3.67, 2.97, 2.97, 3.67, 3.67, 4.3,
4.3, 3.96, 3.96, 3.2, 3.2, 2.41, 2.41, 2.64, 2.64, 3.03, 3.03,
3.82, 3.82, 2.28, 2.28, 4, 4, 3.91, 3.91, 3.27, 3.27, 4.07, 4.07,
4.11, 4.11, 4.47, 4.47, 2.39, 2.39, 5.23, 5.23, 3.43, 3.43, 3.13,
3.13, 3.13, 3.13, 2.55, 2.55, 4.99, 4.99, 3.16, 3.16, 4.05, 4.05,
4.15, 4.15, 2.6, 2.6, 3.54, 3.54, 2.74, 2.74, 1.59, 1.59, 2.79,
2.79, 2.77, 2.77, 3.32, 3.32, 4.3, 4.3, 4.56, 4.56, 2.87, 2.87,
5.29, 5.29, 2.7, 2.7, 2.85, 2.85, 3.55, 3.55, 3.26, 3.26, 3.4,
3.4, 3.49, 3.49, 2.59, 2.59, 3.74, 3.74, 4.24, 4.24, 2.73, 2.73,
2.98, 2.98, 2.87, 2.87, 4.89, 4.89, 3.38, 3.38, 4.35, 4.35, 2.51,
2.51, 4.16, 4.16, 2.99, 2.99, 3.92, 3.92, 2.77, 2.77), ANGPTL8 = c(3337.5,
3962.5, 2737.5, 962.5, 1775, 3737.5, 1025, 962.5, 1175, 912.5,
1662.5, 2075, 2862.5, 1950, 2337.5, 1875, 350, 14412.5, 962.5,
787.5, 1650, 2150, 3250, 1150, 1425, 1162.5, 975, 762.5, 5562.5,
2662.5, 1450, 787.5, 387.5, 475, 1037.5, 1125, 1462.5, 1750,
1137.5, 800, 812.5, 1637.5, 750, 4850, 1112.5, 1187.5, 662.5,
462.5, 4125, 1825, 1275, 750, 6275, 1062.5, 737.5, 3650, 1650,
1425, 2925, 1512.5, 1100, 887.5, 662.5, 825, 487.5, 662.5, 400,
600, 1077.77777777778, 1211.11111111111, 555.555555555556, 511.111111111111,
1066.66666666667, 1311.11111111111, 277.777777777778, 1822.22222222222,
1000, 1055.55555555556, 1255.55555555556, 1000, 1555.55555555556,
1266.66666666667, 1233.33333333333, 1422.22222222222, 1655.55555555556,
800, 555.555555555556, 677.777777777778, 411.111111111111, 344.444444444445,
766.666666666667, 800, 333.333333333333, 1011.11111111111, 455.555555555555,
955.555555555556, 833.333333333333, 777.777777777778, 844.444444444444,
866.666666666667, 755.555555555556, 1011.11111111111, 722.222222222222,
888.888888888889, 255.555555555556, 244.444444444445, 1433.33333333333,
1033.33333333333, 488.888888888889, 477.777777777778, 1600, 1022.22222222222,
1077.77777777778, 988.888888888889, 622.222222222222, 2500, 2077.77777777778,
688.888888888889, 788.888888888889, 1155.55555555556, 1288.88888888889,
1633.33333333333, 1744.44444444445, 2011.11111111111, 366.666666666667,
466.666666666667, 522.222222222222, 1222.22222222222, 477.777777777778,
788.888888888889, 994.444444444445, 1383.33333333333, 2183.33333333333,
661.111111111111, 2350, 1772.22222222222, 672.222222222222, 1183.33333333333,
494.444444444445, 883.333333333333, 416.666666666667, 338.888888888889,
2005.55555555555, 594.444444444444, NA, 305.555555555555, 961.111111111111,
1138.88888888889, 616.666666666667, 583.333333333333, 1405.55555555556,
705.555555555555, 1605.55555555556, 1594.44444444445, 1094.44444444444,
1272.22222222222, 3127.77777777778, 961.111111111111, 750, 661.111111111111,
916.666666666667, 572.222222222222, 1150, 1094.44444444444, 683.333333333333,
827.777777777778, 972.222222222222, 238.888888888889, NA, 327.777777777778,
850, 750, 672.222222222222, 827.777777777778, 983.333333333333,
1038.88888888889), BMP_2 = c(23, 26.92, 25.62, 26.27, 25.62,
26.92, 24.97, 26.92, 25.62, 28.2, NA, 26.92, 22.34, 23, 26.92,
24.32, 24.32, 25.62, 24.32, 25.62, 24.32, 23, 25.62, 28.2, 25.62,
24.32, 23, 26.92, 25.62, 28.2, 24.32, 26.92, 18.95, 23, 23, 25.62,
23, 24.32, 24.32, 23, 25.62, 25.62, 21.67, 26.92, 24.32, 25.62,
21.67, 23, 23, 26.92, 28.2, 24.32, 28.2, 28.2, 26.92, 26.92,
25.62, 25.62, 24.32, 24.32, 24.32, 24.32, 25.62, 23, 17.57, 20.32,
30.61, 27.33, 20.94, 26.16, 23.68, 26.16, 26.16, 28.46, 23.68,
26.16, 20.94, 32.65, 26.16, 28.46, 28.46, 30.61, 26.16, 32.65,
23.68, 28.46, 23.68, 28.46, 19.43, 22.35, 26.16, 28.46, 23.68,
28.46, 26.16, 30.61, 26.16, 28.46, 23.68, 23.68, 28.46, 30.61,
30.61, 30.61, 26.16, 28.46, 20.94, 26.16, 23.68, 30.61, 26.16,
28.46, 20.94, 23.68, 31.64, 26.16, 23.68, 30.61, 23.68, 28.46,
26.16, 30.61, 20.94, 26.16, 14.02, 26.16, 20.94, 23.68, 30.61,
34.58, 23.39, 26.67, 19.74, 19.74, 3, 15.48, 15.48, 23.39, 17.71,
15.48, 15.48, 19.74, 3, 10, NA, 23.39, 19.74, 26.67, 19.74, 19.74,
19.74, 23.39, 17.71, 23.39, 23.39, 26.67, 3, 3, 3, 23.39, 19.74,
19.74, 19.74, 29.69, 33.85, 23.39, 10, 10, 15.48, 23.39, 10,
19.74, 15.48, 15.48, 19.74, 19.74), IGFBP_3_1 = c(441353.12,
NA, 393869.87, NA, NA, NA, 579939.36, NA, 456112.02, NA, NA,
610080.87, NA, NA, 533744.22, 628064.64, 523351.47, NA, 517877.29,
NA, 486315.82, NA, NA, 542659.7, 508437.67, 589967.34, 536282.89,
512564.26, 436271.69, 601179.52, 504448.47, 506264.97, 420330.98,
NA, 538394.66, NA, NA, NA, NA, NA, 495111.88, 549340.97, 672083.18,
NA, 591978.44, NA, NA, 571958.24, 507324.12, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 475288.45, NA, 536037.9, 548109.89,
559995.14, NA, 473616.64, 542571.78, 465343.85, 1127900, 714496.84,
NA, 646959.05, 4856100, 443062.73, 542179.38, 579299.18, 1142900,
564875.53, 1037100, 1174200, NA, 548298.03, 874608.37, 902414.03,
1471500, NA, NA, 1668200, NA, 3153500, 1527000, 534397.71, 556715.71,
1016800, 703025.17, NA, NA, 161911.33, 126486.58, 682462.8, NA,
1365000, NA, 977538.37, NA, 3348600, NA, 1022700, 783787.11,
NA, NA, 859094.87, NA, 1056900, 953743.93, 363547.86, 422392.66,
796697.33, 804929.76, 686250.79, 859712.77, 726741.92, 2091000,
568594.78, 644119.63, 1139000, NA, 802047.77, NA, 1256800, 1442100,
1058500, 974033.9, 967920.77, 981304.96, 1107000, 1197400, 1019800,
1346600, 1135800, 1261900, 1203600, 1352600, NA, 1335400, 1100400,
1398300, 924378.25, 1194500, 1384400, 1186500, 1360700, 1222800,
843925.82, 1232900, 1600800, 1489200, 1133700, 1451700, 1182700,
1445100, 1732100, 1528500, 1321900, 1313500, 1101500, 1422500,
1344700, 1460200, 1224900, 1225100, 1167800, 1155800, 1149200,
1278700)), row.names = c(NA, -176L), class = c("tbl_df", "tbl",
"data.frame"))
您可以使用 !is.na(variable)
仅针对特定变量删除具有 NA
值的行。
library(dplyr)
library(gtsummary)
t2 <-
trial_paired %>%
# delete missing values in variable1
filter(!is.na(variable1)) %>%
# keep IDs with both measurements
group_by(id) %>%
filter(n() == 2) %>%
ungroup() %>%
# summarize data
tbl_summary(by = time1 , include = -id, type = all_continuous() ~ "continuous2", statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min}, {max}", "{mean} ({sd})")) %>%
add_p(test = list(all_continuous() ~ "paired.t.test",
all_categorical() ~ "mcnemar.test"),
group = id)
要动态执行此操作,我们可以创建一个函数。
summary_data <- function(data, var) {
data %>%
# delete missing values
filter(!is.na(.data[[var]])) %>%
# keep IDs with both measurements
group_by(id) %>%
filter(n() == 2) %>%
ungroup() %>%
# summarize data
tbl_summary(by = time1 , include = -id, type = all_continuous() ~ "continuous2", statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min}, {max}", "{mean} ({sd})")) %>%
add_p(test = list(all_continuous() ~ "paired.t.test",
all_categorical() ~ "mcnemar.test"),
group = id)
}
#apply function to single column
summary_data(trial_paired, 'Cholesterol')
summary_data(trial_paired, 'Triglyceride')
#apply function to multiple column
cols <- c('Cholesterol', 'Triglyceride', 'HDL_chol')
#Or drop only the first column
#cols <- names(trial_paired)[-1]
res <- lapply(cols, summary_data, data = trial_paired)