当前位置:网站首页>[R language] two /n data merge functions
[R language] two /n data merge functions
2022-07-01 07:26:00 【Illegal account 247188】
The two data frames are merged
data1=data.frame(
names = c('AAA', 'CCC', 'DDD', 'EEE', 'FFF' ),
logFC = c(3, -0.4, 5, 0.4, -3 ),
bmi = c(' normal ', ' normal ',' overweight ',' obesity ',' normal ')
)
data2=data.frame(
names = c('AAA','BBB', 'CCC', 'DDD', 'EEE' ),
gender = c(1, 2, 1, 2, 1), #1= male 2= Woman
bmi = c(' normal ',' Low weight ',' normal ',' overweight ',' obesity ' )
)
merge(data1,data2,all=TRUE)
# Same result , No need to add by
merge(data1,data2,all=TRUE,by = intersect(names(data1), names(data2)))
names | bmi | logFC | gender |
---|---|---|---|
AAA | normal | 3 | 1 |
BBB | Low weight | NA | 2 |
CCC | normal | -0.4 | 1 |
DDD | overweight | 5 | 2 |
EEE | obesity | 0.4 | 1 |
FFF | normal | -3 | NA |
dplyr::full_join Function case
dplyr::full_join(data1,data2)
dplyr::full_join Function case , If added by, There will be The duplicate variable name is .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 normal 1
2 CCC -0.4 normal 1
3 DDD 5.0 overweight 2
4 EEE 0.4 obesity 1
5 FFF -3.0 normal NA
6 BBB NA Low weight 2
names | logFC | bmi.x | gender | bmi.y |
---|---|---|---|---|
AAA | 3 | normal | 1 | normal |
CCC | -0.4 | normal | 1 | normal |
DDD | 5 | overweight | 2 | overweight |
EEE | 0.4 | obesity | 1 | obesity |
FFF | -3 | normal | NA | NA |
BBB | NA | NA | 2 | Low weight |
R merge()
And 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] |
# Assume the same variable , The contents are different , Such as AAA-BMI- often 1
data2=data.frame(
names = c('AAA','BBB', 'CCC', 'DDD', 'EEE' ),
gender = c(1, 2, 1, 2, 1), #1= male 2= Woman
bmi = c(' normal 1',' Low weight ',' normal ',' overweight ',' obesity ' )
)
merge(data1,data2,all=TRUE,by = intersect(names(data1), names(data2)))
# The result will be a new line
names | bmi | logFC | gender |
---|---|---|---|
AAA | normal | 3 | NA |
AAA | normal 1 | NA | 1 |
BBB | Low weight | NA | 2 |
CCC | normal | -0.4 | 1 |
DDD | overweight | 5 | 2 |
EEE | obesity | 0.4 | 1 |
FFF | normal | -3 | NA |
Merge multiple data frames
data1=data.frame(
names = c('AAA', 'CCC', 'DDD', 'EEE', 'FFF' ),
logFC = c(3, -0.4, 5, 0.4, -3 ),
bmi = c(' normal ', ' normal ',' overweight ',' obesity ',' normal ')
)
data2=data.frame(
names = c('AAA','BBB', 'CCC', 'DDD', 'EEE' ),
gender = c(1, 2, 1, 2, 1), #1= male 2= Woman
bmi = c(' normal ',' Low weight ',' normal ',' overweight ',' obesity ' )
)
data3=data.frame(
names = c('GGG' ),
gender = c(" male "), #1= male 2= Woman
bmi = c(' normal ' )
)
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)
边栏推荐
- LeetCode+ 71 - 75
- Image style migration cyclegan principle
- ctfshow-web352,353(SSRF)
- Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
- The computer has a network, but all browser pages can't be opened. What's the matter?
- Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
- 2022危险化学品经营单位主要负责人试题及模拟考试
- Jax's deep learning and scientific computing
- Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc
- Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
猜你喜欢
Atguigu---- scaffold --02- use scaffold (2)
2022年茶艺师(中级)复训题库及答案
Solution to the problem that objects in unity2021 scene view cannot be directly selected
Custom events of components ①
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
DC-4 target
Is it suitable for girls to study product manager? What are the advantages?
Are there any practical skills for operation and maintenance management
2022制冷与空调设备运行操作国家题库模拟考试平台操作
ctfshow-web354(SSRF)
随机推荐
Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
图像风格迁移 CycleGAN原理
redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
C language implementation [Sanzi chess game] (step analysis and implementation source code)
LeetCode+ 71 - 75
[the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
Custom events of components ②
Autosar 学习记录(1) – EcuM_Init
Microsoft announces open source (Godel) language model chat robot
AUTOSAR learning record (1) – ECUM_ Init
redisson使用全解——redisson官方文档+注释(上篇)
Ctfhub port scan (SSRF)
[programming training 2] sorting subsequence + inverted string
1286_FreeRTOS的任务优先级设置实现分析
redisson使用全解——redisson官方文档+注释(下篇)
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
Unity2021-Scene视图中物体无法直接选中的解决办法
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
盘点华为云GaussDB(for Redis)六大秒级能力
atguigu----脚手架--02-使用脚手架(2)