当前位置:网站首页>[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)
边栏推荐
- Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc
- 赌上了绩效,赢了公司CTO,我要搭DevOps平台!
- Mysql与Redis一致性解决方案
- redisson使用全解——redisson官方文檔+注釋(上篇)
- ctfshow-web355,356(SSRF)
- Eigen矩阵运算库快速上手
- LeetCode+ 71 - 75
- 华为ModelArts训练Alexnet模型
- Subclasses call methods and properties of the parent class with the same name
- ctfshow-web352,353(SSRF)
猜你喜欢

Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect

Unity2021-Scene视图中物体无法直接选中的解决办法

ctfshow-web352,353(SSRF)

ctfhub-端口扫描(SSRF)

运维面临挑战?智能运维管理系统来帮您

LeetCode+ 71 - 75

Why did grayscale fall from the altar?

Todolist classic case ①
![[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

LeetCode+ 71 - 75
随机推荐
How to choose a product manager course when changing to a product manager?
[matlab] solve nonlinear programming
熱烈祝賀五行和合酒成功掛牌
How to create an exclusive vs Code theme
【系统分析师之路】第五章 复盘软件工程(逆向净室与模型驱动开发)
運維管理系統,人性化操作體驗
Which securities company is better or safer for mobile phone account opening
Why are so many people turning to product managers? What is the development prospect of product manager?
Félicitations pour l'inscription réussie de wuxinghe
C# Newtonsoft. Use of job in JSON
【技能】创建.bat快速打开网页
atguigu----脚手架--02-使用脚手架(2)
go-etcd
【图像处理】图像直方图均衡化系统含GUI界面
Redisson uses the full solution - redisson official documents + comments (Part 2)
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account