我有一个大的数据帧,包含多个二分变量。前10行是这样的:
structure(list(subject_sp_id = c("SP0189536", "SP0287404",
"SP0268021", "SP0313735", "SP0050394", "SP0173973", "SP0035655",
"SP0360624", "SP0153386", "SP0229810"), sex = c("Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Female"), attn1 = c(0L, 1L,
1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), adhd1 = c(0L, 1L, 1L,
0L, 0L, 1L, 1L, 1L, 1L, 1L), conduct1 = c(0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), intermitt_explos1 = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), behav_odd1 = c(0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 0L), devid1 = c(1L, 0L, 0L, 0L, 0L, 1L,
1L, 0L, 0L, 0L), dev1 = c(1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L,
1L, 1L), lang_dis1 = c(1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L,
0L), ld1 = c(1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L), motor1 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mutism1 = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), soc_prag1 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L), dev_speech1 = c(0L, 0L, 0L, 0L,
0L, 1L, 0L, 0L, 1L, 0L), eating_probs1 = c(0L, 1L, 0L, 0L, 0L,
1L, 0L, 1L, 1L, 0L), eating_disorder1 = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L), encopres1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), enures1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), feeding_dx1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L),
mood_anx1 = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L), mood_bipol1 = c(0L,
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mood_dep1 = c(0L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mood_dmd1 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), mood_hoard1 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), mood_ocd1 = c(1L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), mood_or_anx1 = c(1L, 0L, 1L, 1L,
0L, 0L, 0L, 1L, 1L, 1L), mood_sep_anx1 = c(0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), mood_soc_anx1 = c(0L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 1L), neuro_sz1 = c(0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L), pers_dis1 = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L), schiz1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), tics1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), attn2 = c(0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L
), adhd2 = c(0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L
), conduct2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), intermitt_explos2 = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L), behav_odd2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 0L), devid2 = c(1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L,
1L, 0L), dev2 = c(1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), lang_dis2 = c(1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L,
1L), ld2 = c(1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L),
motor2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mutism2 = c(1L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), soc_prag2 = c(0L,
0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L), dev_speech2 = c(0L,
0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), eating_probs2 = c(0L,
0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), eating_disorder2 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), encopres2 = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), enures2 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), feeding_dx2 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), mood_anx2 = c(0L, 0L, 1L, 0L,
0L, 0L, 0L, 1L, 0L, 1L), mood_bipol2 = c(1L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), mood_dep2 = c(0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 1L), mood_dmd2 = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L), mood_hoard2 = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L), mood_ocd2 = c(0L, 0L, 1L, 0L, 0L, 1L, 0L,
1L, 1L, 1L), mood_or_anx2 = c(1L, 0L, 1L, 0L, 0L, 1L, 0L,
1L, 1L, 1L), mood_sep_anx2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L), mood_soc_anx2 = c(0L, 0L, 1L, 0L, 0L, 1L, 0L,
0L, 0L, 1L), neuro_sz2 = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L), pers_dis2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), schiz2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
tics2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), sex2 = c("Female",
"Female", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male"), age_band = structure(c(8L, 8L, 8L,
8L, 8L, 8L, 8L, 7L, 7L, 6L), .Label = c("0-3", "3-6", "6-9",
"9-12", "12-15", "15-18", "18-21", "21-31", "31+"), class = "factor")), row.names = c(NA,
10L), class = "data.frame")
现在,我想计算两个测量点1和2之间每个变量(Va、Vb、Vc(的一致率,以及年龄和性别的协变量,即每个项目的匹配/不匹配百分比。
在尝试了Roman的琐碎答案后,我得到了这样的输出:
structure(list(gr1 = c("attn_2", "adhd2", "conduct2",
"intermitt_explos2", "behav_odd2", "devid2", "lang_dis2",
"dev", "ld2", "motor2", "mutism2", "soc_prag2",
"dev_speech2", "eating_disorder2", "eating_probs2", "encopres2",
"enures2", "feeding_dx2", "mood_anx2", "mood_bipol2"), `0-3` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), `3-6` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), `6-9` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), `9-12` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), `12-15` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), `15-18` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), `18-21` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), `21-31` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), `31+` = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
你知道我为什么会得到这样的输出吗?我应该如何解决它?
如果我做对了,你可以尝试tidyverse
library(tidyverse)
df %>%
pivot_longer(-1:-3, names_to = c("gr1", "gr2"), names_sep = 2) %>%
group_by(ID, age, gr1) %>%
summarise(same=sum(length(unique(value))==1)) %>%
group_by(age, gr1) %>%
summarise(perc=sum(same)/n()) %>%
pivot_wider(names_from = age, values_from = perc, values_fn = unique)
# A tibble: 3 x 4
gr1 `18-25` `25-30` `30+`
<chr> <dbl> <dbl> <dbl>
1 Va 0.5 0.5 1
2 Vb 0 0.5 0
3 Vc 0.5 0 1
只需更改所有";年龄;至";性;针对特定性别的价值观。