当前位置:网站首页>[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)
边栏推荐
- Illusory and simple screen raindrop post-processing effect
- [FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
- MySQL and redis consistency solution
- JAX的深度学习和科学计算
- 如何让两融交易更极速
- Why did grayscale fall from the altar?
- Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
- Vscode automatically formats code according to eslint specification
- Redisson uses the full solution - redisson official document + comments (Part 2)
- Is it safe to do fund fixed investment on Great Wall Securities?
猜你喜欢

如何让两融交易更极速
![[image processing] image histogram equalization system with GUI interface](/img/3b/52c241c48c91dd58300af58478129e.png)
[image processing] image histogram equalization system with GUI interface

Operation and maintenance management system, humanized operation experience

redisson使用全解——redisson官方文檔+注釋(上篇)
![[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario](/img/10/ed857892d2e0ea72e100a4008e6d69.png)
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario

The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly

组件的自定义事件①

DC-4靶机

ctfshow-web355,356(SSRF)

redisson使用全解——redisson官方文档+注释(中篇)
随机推荐
她就是那个「别人家的HR」|ONES 人物
Why did grayscale fall from the altar?
Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
熱烈祝賀五行和合酒成功掛牌
Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée
Redisson uses the complete solution - redisson official documents + Notes (Part 1)
[Shenzhen IO] precise Food Scale (some understanding of assembly language)
Oracle创建自增id
Félicitations pour l'inscription réussie de wuxinghe
2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
关于“2022年度网络安全教育线上培训”相关问题的复盘和说明
2022电工(中级)复训题库及答案
Vscode automatically formats code according to eslint specification
JSP - paging
Kdtree notes
How to choose a product manager course when changing to a product manager?
Fix the problem that the AI video intelligent platform easycvr device video cannot be played
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
图像风格迁移 CycleGAN原理
kdtree(kd树)笔记