当前位置:网站首页>[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)
边栏推荐
- weback5基础配置详解
- How to draw a product architecture diagram?
- ctfshow-web351(SSRF)
- Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
- The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
- 未来互联网人才还稀缺吗?哪些技术方向热门?
- Why are so many people turning to product managers? What is the development prospect of product manager?
- Jax's deep learning and scientific computing
- ctfshow-web352,353(SSRF)
- C language implementation [minesweeping game] full version (implementation source code)
猜你喜欢
Paging in servlets and JSPS
ctfshow-web355,356(SSRF)
Todolist classic case ①
[programming training] delete public characters (hash mapping) + team competition (greedy)
Custom events of components ①
2022危险化学品经营单位主要负责人试题及模拟考试
redisson使用全解——redisson官方文檔+注釋(上篇)
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
MySQL and redis consistency solution
Redisson uses the complete solution - redisson official documents + Notes (Part 1)
随机推荐
Servlet 和 JSP 中的分页
[image processing] image histogram equalization system with GUI interface
go-etcd
Minecraft 1.16.5 module development (51) tile entity
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
【微服务|openfeign】Feign的日志记录
Solution to the problem that objects in unity2021 scene view cannot be directly selected
Reply and explanation on issues related to "online training of network security education in 2022"
2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
C# Newtonsoft. Use of job in JSON
Vscode automatically formats code according to eslint specification
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components
Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
运维管理有什么实用的技巧吗
Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
【图像处理】图像直方图均衡化系统含GUI界面
Operation and maintenance management system, humanized operation experience
go-etcd
1286_ Implementation analysis of task priority setting in FreeRTOS
2022电工(中级)复训题库及答案