匹配不同数据帧上的多个列,并获得其他列作为结果 [英] Matching multiple columns on different data frames and getting other column as result

查看:115
本文介绍了匹配不同数据帧上的多个列,并获得其他列作为结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个大数据框,一个( df1 )有这样的结构

  chr init 
1 12 25289552
2 3 180418785
3 3 180434779

另一个( df2 )有这个

  V1 V2 V3 
10 1 69094 medium
11 1 69094 medium
12 12 25289552 high
13 1 69095 medium
14 3 180418785 medium
15 3 180434779低

我要做的是添加列 V3 df2 df1 ,以获取变化的信息

  chr init Mut 
1 12 25289552 high
2 3 180418785 medium
3 3 180434779 low

我正在尝试将R加载到R中,然后使用match进行for循环,但不起作用。你有什么特别的做法吗?我也可以使用awk或类似的东西。

解决方案

使用 merge

  df1<  -  read.table(text ='chr init 
1 12 25289552
2 3 180418785
3 3 180434779',header = TRUE)


df2< - read.table(text ='V1 V2 V3
10 1 69094 medium
11 1 69094 medium
12 12 25289552 high
13 1 69095 medium
14 3 180418785 medium
15 3 180434779 low',header = TRUE)


merge(df1,df2,by.x ='init',by.y ='V2')#这个工程!
init chr V1 V3
1 25289552 12 12高
2 180418785 3 3中等
3 180434779 3 3低

以您的方式显示您想要的输出

 输出< ;  -  merge(df1,df2,by.x ='init',by.y ='V2')[,c(2,1,4)] 
colnames(output)[3]< Mut'
输出
chr init Mut
1 12 25289552 high
2 3 180418785 medium
3 3 180434779 low
/ pre>

I got two big data frames, one (df1) has this structure

   chr    init
1  12  25289552
2   3 180418785
3   3 180434779

The other (df2) has this

    V1    V2     V3
10  1     69094 medium
11  1     69094 medium
12  12 25289552 high
13  1     69095 medium
14  3 180418785 medium
15  3 180434779 low

What I'm trying to do is to add the column V3 of df2 to df1, to get the info of the mutation

   chr    init  Mut
1  12  25289552 high
2   3 180418785 medium
3   3 180434779 low

I'm trying loading both into R and then doing a for loop using match but it doesn't work. Do you know any special way to do this? I am also open to do using awk or something similar

解决方案

Use merge

df1 <- read.table(text='  chr    init
1  12  25289552
2   3 180418785
3   3 180434779', header=TRUE)


df2 <- read.table(text='    V1    V2     V3
10  1     69094 medium
11  1     69094 medium
12  12 25289552 high
13  1     69095 medium
14  3 180418785 medium
15  3 180434779 low', header=TRUE)


merge(df1, df2, by.x='init', by.y='V2') # this works!
       init chr V1     V3
1  25289552  12 12   high
2 180418785   3  3 medium
3 180434779   3  3    low

To get your desired output the way you show it

output <- merge(df1, df2, by.x='init', by.y='V2')[, c(2,1,4)]
colnames(output)[3] <- 'Mut' 
output
  chr      init    Mut
1  12  25289552   high
2   3 180418785 medium
3   3 180434779    low

这篇关于匹配不同数据帧上的多个列,并获得其他列作为结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆