1. library(dplyr)
  2. unite(mtcars, "vs_am", vs, am)

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

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

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

 
分类: [31]R

r里面如何实现两列数据合并为一列的更多相关文章

  1. Kettle 行列互换之——行转列(多列数据合并成一列变为多行)

    原始需求如下: 业务系统设置成这样,见截图. 工资项目为了方便录入,都是做成列的. 但是这些数据需要和另外的费用报销系统的数据关联,费用报销系统的费用项目是横向的,用费用项目.金额的多行来表达.那么这 ...

  2. sql查询一个字段多列值合并为一列

    SELECT GROUP_CONCAT(A.字段) AS 字段别名 FROM 表名 A WHERE A.字段=,,) SELECT GROUP_CONCAT(A.字段) AS 字段FROM 表名 A

  3. Expression构建DataTable to Entity 映射委托 sqlserver 数据库里面金额类型为什么不建议用float,实例告诉你为什么不能。 sql server 多行数据合并成一列 C# 字符串大写转小写,小写转大写,数字保留,其他除外 从0开始用U盘制作启动盘装Windows10系统(联想R720笔记本)并永久激活方法 纯CSS打造淘宝导航菜单栏 C# Winform

    Expression构建DataTable to Entity 映射委托   1 namespace Echofool.Utility.Common { 2 using System; 3 using ...

  4. 使用FOR XML PATH实现多行数据合并成一列

    有时为避免循环操作数据库.列表展示等一些原因需要将数据及关联数据批量加载进行集中处理,一种解决办法可以使用FOR XML PATH将多行数据合并成一列,达到字段拼接的效果.例如有两个表, 部门表T_D ...

  5. GROUP_CONCAT 将mysql多条数据合并为一条

    实现将多条数据合并为一条数据,在mysql中可以通过 GROUP_CONCAT 函数实现 上面是潇leader发我的和工作不相关的小小小需求描述,很明显是要把id和name相同的数据合并为一条,下面按 ...

  6. SQL 一列数据整合为一条数据

    SQL 一列数据整合为一条数据: SELECT  STUFF(( SELECT distinct  ',' + 列名 FROM 表名 where  [条件] FOR XML PATH('') ), 1 ...

  7. R之data.table -melt/dcast(数据合并和拆分)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 30.0px "Helvetica Neue"; color: #323333 } p. ...

  8. SQL两列数据,行转列

    SQL中只有两列数据(字段1,字段2),将其相同字段1的行转列 转换前: 转换后: --测试数据 if not object_id(N'Tempdb..#T') is null drop table ...

  9. SQL SERVER 如何把1列多行数据 合并成一列显示

    示例 修改前:1列多行数据 修改后:合并成一列 示例语句 1 2 3 4 5 6 7 8 9 10 11 select 类别,     名称 = (         stuff(            ...

随机推荐

  1. IntelliJ IDEA使用教程(简介)

    最智能的IDE IDEA 全称IntelliJ IDEA   是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支 ...

  2. JavaScript踩坑

    1 //这样做会抛出错误 alert(ttt); //这样做不会,只是会弹出undefine而已 alert(window.ttt); 当然可以try catch如此捕获异常 try { //这样做会 ...

  3. python系统编程(三)

    multiprocessing 如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择.由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序? 由 ...

  4. Django——日志

    日志级别 5 个级别 debug 调试 info 普通信息 warning : 提醒警告 error: 发生了错误 critical: 严重的错误 在settings中添加: LOGGING = { ...

  5. egret.Shape渲染集合图形

    代码: class Main extends egret.DisplayObjectContainer { public constructor() { super(); this.addEventL ...

  6. PAT Basic 1007

    1007 素数对猜想 (20 分) 让我们定义d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数.显然有d​1​​=1,且对于n>1有d​n​​是偶数.“素数对猜想 ...

  7. parcel 中小型项目打包工具

    "0配置"打包器(bundler)Parcel Parcel官网(有中文文档) webpack 要有大量的配置,这样带来的成本就是复杂性--与此相对的,Parcel 带来了简洁性. ...

  8. python之函数联系

    ----------------------作业一 # 有两个列表,分别存放来老男孩报名学习linux和python课程的学生名字# linux=['钢弹','小壁虎','小虎比','alex','w ...

  9. C_求最大连续子序列和

    题目: 输入一组整数,求出这组数字子序列和中最大值.也就是只要求出最大子序列的和,不必求出最大的那个序列.例如: 序列:-2 11 -4 13 -5 -2,则最大子序列和为20. 序列:-6 2 4 ...

  10. Impala Apache Hadoop 安装方法

    http://blog.csdn.net/mayp1/article/details/50952512