当前位置:网站首页>[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
2022-07-01 07:27:00 【Illegal account 247188】
Case control study , Frequency matching of age and sex
Example data
| ID | group | sex | age |
|---|---|---|---|
| A1 | Control | Woman | 90 |
| A10 | Control | male | 89 |
| A100 | Control | male | 85 |
| A1000 | Control | male | 74 |
| A1001 | Case | Woman | 74 |
| A1002 | Case | male | 74 |
| A1003 | Case | Woman | 74 |
| A1004 | Case | male | 74 |
| A1005 | Control | male | 74 |
data <- read.csv(" Age sex frequency matching .csv")
## Age can be stratified first
data$agegroup <- cut(data$age,c(0,10,20,30,40,50,60,70,80,90,Inf),
right =FALSE # Select section opening [ ] situation
)
# Generate list data , By each list[?] For the same reason sex and age
dataSplit <- split(data, list(data$sex, data$age))
# Single list extract
A <- as.data.frame(data_case[[1]])
data1 <- A[A$group == "Case", ]
data2 <- A[A$group == "Control", ]
num <- as.numeric(table(A$group == "Case")[2])
set.seed(123) # Set random number , Repeat many times to keep consistent
data3 <- data2[sample(1:NROW(data2), num, replace = FALSE), ]
assign(paste0("dataFinal", 1), rbind(data1, data3))
# Cyclic extraction
for (i in 1:length(dataSplit)) {
A <- as.data.frame(data_case[[i]])
data1 <- A[A$group == "Case", ]
data2 <- A[A$group == "Control", ]
numCase <- nrow(data1)
numControl <- nrow(data2)
if (numCase > 0 & numControl>0 & numControl-numCase>=0) {
na.omit <- TRUE
set.seed(123) # Set random number , Repeat many times to keep consistent 【 Can be silent 】
data3 <- data2[sample(1:numControl, numCase, replace = FALSE), ]
assign(paste0("dataFinal", i), rbind(data1, data3))
}
if (numCase > 0 & numControl>0 & numControl-numCase<0) {
na.omit <- TRUE
set.seed(123) # Set random number , Repeat many times to keep consistent 【 Can be silent 】
data3 <- data1[sample(1:numCase, numControl, replace = FALSE), ]
assign(paste0("dataFinal", i), rbind(data2, data3))
}
}
# Merge multiple data frames
multimerge<-function(dat=list(),...){
if(length(dat)<2)return(as.data.frame(dat))
mergedat<-dat[[1]]
dat[[1]]<-NULL
for(i in dat){
mergedat<-merge(all=TRUE,mergedat,i,...)
}
return(mergedat)
}
files=ls(pattern = "dataFinal")
listALL=list()
# Data frame composition list
for (i in 1:length(ls(pattern = "dataFinal"))) {
eval(parse(text = paste0("listALL","[[",i,"]]", " <- ",files[i])))
}
dataALL <- multimerge(listALL)
# Sort
dataALL=dplyr::arrange(dataALL,age,sex,group)
# export
write.csv(dataALL,"dataALL.csv",row.names = F)
边栏推荐
- 1286_ Implementation analysis of task priority setting in FreeRTOS
- Do securities account opening affect the security of account opening
- redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
- Redisson uses the full solution - redisson official document + comments (Part 2)
- Which securities company is better or safer for mobile phone account opening
- Redisson uses the full solution - redisson official documents + comments (Part 2)
- Autosar 学习记录(1) – EcuM_Init
- 2022茶艺师(初级)操作证考试题库及模拟考试
- Ctfhub port scan (SSRF)
- 盘点华为云GaussDB(for Redis)六大秒级能力
猜你喜欢

ctfshow-web355,356(SSRF)

Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée

2022茶艺师(初级)操作证考试题库及模拟考试

Pourquoi tant de gens sont - ils devenus des gestionnaires de produits? Quelles sont les perspectives de développement des gestionnaires de produits?
![C language implementation [minesweeping game] full version (implementation source code)](/img/70/60f9a61bd99fa5fb5fab679a32528e.png)
C language implementation [minesweeping game] full version (implementation source code)
![[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines](/img/34/d2efae5b283cdc130d55f52cdff76d.png)
[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines

熱烈祝賀五行和合酒成功掛牌

Illusory and simple screen raindrop post-processing effect

Paging in servlets and JSPS

2022危险化学品经营单位主要负责人试题及模拟考试
随机推荐
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)
Autosar 学习记录(1) – EcuM_Init
手机开户选哪个证券公司比较好,哪个更安全
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
JAX的深度学习和科学计算
go-etcd
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
TodoList经典案例①
Browser local storage
图像风格迁移 CycleGAN原理
AUTOSAR learning record (1) – ECUM_ Init
Why did grayscale fall from the altar?
微软宣布开源 (GODEL) 语言模型聊天机器人
北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
[programming training] delete public characters (hash mapping) + team competition (greedy)
Solution to the problem that objects in unity2021 scene view cannot be directly selected
她就是那个「别人家的HR」|ONES 人物