当前位置:网站首页>【R语言】两个/N个数据合并merge函数
【R语言】两个/N个数据合并merge函数
2022-07-01 07:18:00 【违规账号247188】
两个数据框合并
data1=data.frame(
names = c('AAA', 'CCC', 'DDD', 'EEE', 'FFF' ),
logFC = c(3, -0.4, 5, 0.4, -3 ),
bmi = c('正常', '正常','超重','肥胖','正常')
)
data2=data.frame(
names = c('AAA','BBB', 'CCC', 'DDD', 'EEE' ),
gender = c(1, 2, 1, 2, 1), #1=男 2=女
bmi = c('正常','低体重','正常','超重','肥胖' )
)
merge(data1,data2,all=TRUE)
# 同样结果,可以不用加by
merge(data1,data2,all=TRUE,by = intersect(names(data1), names(data2)))
| names | bmi | logFC | gender |
|---|---|---|---|
| AAA | 正常 | 3 | 1 |
| BBB | 低体重 | NA | 2 |
| CCC | 正常 | -0.4 | 1 |
| DDD | 超重 | 5 | 2 |
| EEE | 肥胖 | 0.4 | 1 |
| FFF | 正常 | -3 | NA |
dplyr::full_join函数情况
dplyr::full_join(data1,data2)
dplyr::full_join函数情况,如果加by,会出现重复的变量名为 .x .y
dplyr::full_join(data1,data2,by=c("names"))
> dplyr::full_join(data1,data2)
Joining, by = c("names", "bmi")
names logFC bmi gender
1 AAA 3.0 正常 1
2 CCC -0.4 正常 1
3 DDD 5.0 超重 2
4 EEE 0.4 肥胖 1
5 FFF -3.0 正常 NA
6 BBB NA 低体重 2
| names | logFC | bmi.x | gender | bmi.y |
|---|---|---|---|---|
| AAA | 3 | 正常 | 1 | 正常 |
| CCC | -0.4 | 正常 | 1 | 正常 |
| DDD | 5 | 超重 | 2 | 超重 |
| EEE | 0.4 | 肥胖 | 1 | 肥胖 |
| FFF | -3 | 正常 | NA | NA |
| BBB | NA | NA | 2 | 低体重 |
R merge() 与dplyr join()
| dplyr | base |
|---|---|
| inner_join(df1, df2) | merge(df1, df2) |
| left_join(df1, df2) | merge(df1, df2, all.x = TRUE) |
| right_join(df1, df2) | merge(df1, df2, all.y = TRUE) |
| full_join(df1, df2) | merge(df1, df2, all = TRUE) |
| semi_join(df1, df2) | df1[df1 x x %in% df2 xx, , drop = FALSE] |
| anti_join(df1, df2) | df1[!df1 x x %in% df2 xx, , drop = FALSE] |
# 假设相同变量,里面得内容不一样,如AAA-BMI-常1
data2=data.frame(
names = c('AAA','BBB', 'CCC', 'DDD', 'EEE' ),
gender = c(1, 2, 1, 2, 1), #1=男 2=女
bmi = c('正常1','低体重','正常','超重','肥胖' )
)
merge(data1,data2,all=TRUE,by = intersect(names(data1), names(data2)))
# 结果会另起一行
| names | bmi | logFC | gender |
|---|---|---|---|
| AAA | 正常 | 3 | NA |
| AAA | 正常1 | NA | 1 |
| BBB | 低体重 | NA | 2 |
| CCC | 正常 | -0.4 | 1 |
| DDD | 超重 | 5 | 2 |
| EEE | 肥胖 | 0.4 | 1 |
| FFF | 正常 | -3 | NA |
多个数据框合并
data1=data.frame(
names = c('AAA', 'CCC', 'DDD', 'EEE', 'FFF' ),
logFC = c(3, -0.4, 5, 0.4, -3 ),
bmi = c('正常', '正常','超重','肥胖','正常')
)
data2=data.frame(
names = c('AAA','BBB', 'CCC', 'DDD', 'EEE' ),
gender = c(1, 2, 1, 2, 1), #1=男 2=女
bmi = c('正常','低体重','正常','超重','肥胖' )
)
data3=data.frame(
names = c('GGG' ),
gender = c("男"), #1=男 2=女
bmi = c('正常' )
)
file=ls(pattern = "data")
ALL1=list(data1,data2,data3)
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)
}
multimerge(ALL1)
边栏推荐
- Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée
- 【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
- ctfshow-web354(SSRF)
- Custom events of components ①
- Illusory and simple screen raindrop post-processing effect
- Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
- C# Newtonsoft. Use of job in JSON
- go-etcd
- Redisson uses the full solution - redisson official documents + comments (Part 2)
- Fix the problem that the AI video intelligent platform easycvr device video cannot be played
猜你喜欢

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

Illusory and simple screen raindrop post-processing effect

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

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

【LINGO】求无向图的最短路问题

继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?

Microsoft announces open source (Godel) language model chat robot
![[programming training 2] sorting subsequence + inverted string](/img/96/87750c5d3954ef6c39cce073e8b9ae.png)
[programming training 2] sorting subsequence + inverted string

ctfshow-web352,353(SSRF)
![C language implementation [minesweeping game] full version (implementation source code)](/img/70/60f9a61bd99fa5fb5fab679a32528e.png)
C language implementation [minesweeping game] full version (implementation source code)
随机推荐
Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
Operation and maintenance management system, humanized operation experience
图像风格迁移 CycleGAN原理
2022年流动式起重机司机考试练习题及在线模拟考试
EasyNVS云管理平台功能重构:支持新增用户、修改信息等
组件的自定义事件①
【微服务|openfeign】Feign的日志记录
MySQL and redis consistency solution
Reply and explanation on issues related to "online training of network security education in 2022"
【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
5G Massive MIMO的概念和优点总结
Is it safe to do fund fixed investment on Great Wall Securities?
[classification model] Q-type cluster analysis
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
Browser local storage
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
【MATLAB】求解非线性规划
组件的自定义事件②
【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account