Merging Data

Adding Columns

To merge two data frames (datasets) horizontally,  use the merge function. In most cases, you join two data frames  by one or more common key variables (i.e., an inner join).

# merge two data frames by ID  

total <- merge(data frameA,data frameB,by="ID") #by指定的列中的值必须是唯一的,不能重复出现两行有相同的ID

# merge two data frames by ID and Country  

total <- merge(data frameA,data frameB,by=c("ID","Country")) #by指定的列中的值必须是唯一的,不能重复出现两行有相同的ID

Inner join: merge(df1, df2) will work for these examples because R automatically joins the frames by common variable names, but you would most likely want to specify merge(df1, df2, by="CustomerId") to make sure that you were matching on only the fields you desired.  You can also use the by.x and by.y parameters if the matching variables have different names in the different data frames.

Outer join: merge(x = df1, y = df2, by = "CustomerId", all = TRUE) #by指定的列中的值必须是唯一的,不能重复出现两行有相同的ID

Left outer: merge(x = df1, y = df2, by = "CustomerId", all.x=TRUE) #by指定的列中的值必须是唯一的,不能重复出现两行有相同的ID

Right outer: merge(x = df1, y = df2, by = "CustomerId", all.y=TRUE) #by指定的列中的值必须是唯一的,不能重复出现两行有相同的ID

Cross join: merge(x = df1, y = df2, by = NULL) #by指定的列中的值必须是唯一的,不能重复出现两行有相同的ID

#########################

> df2 = data.frame(CustomerId=c(2,4,6),State=c(rep("Alabama",2),rep("Ohio",1)))

> df1

CustomerId Product

1          1 Toaster

2          2 Toaster

3          3 Toaster

4          4   Radio

5          5   Radio

6          6   Radio

> df2

CustomerId   State

1          2 Alabama

2          4 Alabama

3          6    Ohio

> merge(df1, df2, all=TRUE)

CustomerId Product   State

1          1 Toaster    <NA>

2          2 Toaster Alabama

3          3 Toaster    <NA>

4          4   Radio Alabama

5          5   Radio    <NA>

6          6   Radio    Ohio

> merge(df1, df2, all.x=TRUE)

CustomerId Product   State

1          1 Toaster    <NA>

2          2 Toaster Alabama

3          3 Toaster    <NA>

4          4   Radio Alabama

5          5   Radio    <NA>

6          6   Radio    Ohio

> merge(df1, df2, all.y=TRUE)

CustomerId Product   State

1          2 Toaster Alabama

2          4   Radio Alabama

3          6   Radio    Ohio

#####################################

de <- merge(d, e, by=0, all=TRUE) # merge by row names (by=0 or by="row.names")

REF:

http://stat.ethz.ch/R-manual/R-devel/library/base/html/merge.html

http://www.statmethods.net/management/merging.html

http://stackoverflow.com/questions/1299871/how-to-join-data-frames-in-r-inner-outer-left-right

http://blog.sciencenet.cn/blog-508298-652589.html

R语言合并data.frame的更多相关文章

  1. 使用R语言-操作data.frame

    1 向一个data.frame指定列插入一列新数据 1.1 插入一列到指定位置 y<-1:4 data1 <-data.frame(x1=c(1,3,5,7), x2=c(2,4,6,8) ...

  2. R语言基础-data.frame

    data.frame比较像表格,每一列是一个向量,即每列中的元素是同一类型:所有列具有相同的长度. x = 10:1 y = -4:5 q = c("Ha","oh&qu ...

  3. 如何将R中的data frame对象的数据导入到DB

    在使用ARIMA模型来预测我们的销量的时候,如果保存预测版本进DB,以供后续分析呢 1. 在定义变量阶段我们定义了dfResult      <- data.frame() 这是一个data f ...

  4. R list和data frame 排序

    pathway_name = rownames(g1) tm <- list('P-value' = c(), 'Pathway_name' = c()) :dim(g1)[]){ result ...

  5. R语言︱情感分析—基于监督算法R语言实现(二)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 笔者寄语:本文大多内容来自未出版的<数据 ...

  6. R语言︱非结构化数据处理神器——rlist包

    本文作者:任坤,厦门大学王亚南经济研究院金融硕士生,研究兴趣为计算统计和金融量化交易,pipeR,learnR,rlist等项目的作者. 近年来,非关系型数据逐渐获得了更广泛的关注和使用.下面分别列举 ...

  7. R语言CSV文件

    R语言CSV文件 在R语言中,我们可以从存储在R环境外部的文件读取数据.还可以将数据写入由操作系统存储和访问的文件. R可以读取和写入各种文件格式,如:csv,excel,xml等. 在本章中,我们将 ...

  8. R语言︱list用法、批量读取、写出数据时的用法

    列表是一种特别的对象集合,它的元素也由序号(下标)区分,但是各元素的类型可 以是任意对象,不同元素不必是同一类型.元素本身允许是其它复杂数据类型,比如,列表 的一个元素也允许是列表.例如: > ...

  9. R语言Data Frame数据框常用操作

    Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可 ...

随机推荐

  1. JQuery设置获取下拉菜单选项的值 多实例

    分享下JQuery如何设置获取下拉菜单某个选项的值,多种方法,值得收藏. JQuery获取和设置Select选项 获取Select :获取select 选中的 text :$(“#ddlRegType ...

  2. Atitit 跨平台的系统截图解决方案

    Atitit 跨平台的系统截图解决方案 1.1. Nodes js 方案desktop-screenshot进行系统截图1 1.2. Win 方案,autoit dsl,可能不跨台1 1.3. Jav ...

  3. FullWebBrowserCookie 取得WebBrowser的完整Cookie

    using System; using System.ComponentModel; using System.Net; using System.Runtime.InteropServices; u ...

  4. Merge into 详细介绍

    /*Merge into 详细介绍 MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句. 通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询, 连接条 ...

  5. 菜鸟学SSH(七)——Spring jar包详解

    Struts.Hibernate.Spring这类的框架给我们开发带来非常大的好处,让我们更加快速.有效的开发.所以我们在开发中通常都会用到各种框架,每个框架都有很多jar包,每个jar都有各自不同的 ...

  6. 【小白的CFD之旅】21 网格划分软件的选择

    但是怎样才能获得流体计算网格呢?“工欲善其事必先利其器”,画网格该用什么器呢?小白决定找黄师姐请教一番. 小白找到黄师姐的时候,黄师姐正在电脑上忙着. “黄师姐,我发现网格划分软件有好多种,究竟哪种才 ...

  7. [SQL in Azure] Getting Started with SQL Server in Azure Virtual Machines

    This topic provides guidelines on how to sign up for SQL Server on a Azure virtual machine and how t ...

  8. 每日英语:Three Shows That Changed The Way Networks Think About Viewership

    As we continue examining this season’s DVR success stories in The Blacklist and Sleepy Hollow it mak ...

  9. DLL中获取主程序句柄

    一.问题由来及说明 在实际编程中遇到一些系统模块(或API)调用初始化需要窗口句柄,在主程序中(MFC或者win32)都可以很容易获得,比如mfc中可以直接调用AfxGetMainWnd().GetS ...

  10. LeetCode: Jump Game Total 解题报告

    Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...