美文网首页
数据整合和数据清洗

数据整合和数据清洗

作者: 蓝色滑行 | 来源:发表于2018-12-08 22:54 被阅读0次

1.数据整合

使用SQL语句,structured query language 结构化查询语言,是一种面向对象的语言,又是嵌入式语言,既可以独立使用也可以嵌入到宿主语言中。
install.packages('sqldf') # R中使用sql语言需要安装sqldf包
library(sqldf)
setwd("D:/《用商业案例学R语言数据挖掘》教材代码及数据/data") #设定工作路径
sale <- read.csv("sale.csv")
sqldf("select year,market,sale,profit from sale") #选择字段并展示

year market sale profit
1 2010 东 33912 2641
2 2010 南 32246 2699
3 2010 西 34792 2574
4 2010 北 31884 2673
5 2011 东 31651 2437
6 2011 南 30572 2853
7 2011 西 34175 2877
8 2011 北 30555 2749
9 2012 东 31619 2106
10 2012 南 32443 3124
11 2012 西 32103 2593
12 2012 北 31744 2962

选择一列数据(year),并删除重复值,展示销售数据中的不同年份,使用DISTINCT删除查询结果中的重复行
sqldf("select DISTINCT year from sale")

year
1 2010
2 2011
3 2012

筛选数据,使用where语句查询结果满足要求的行。显示2012年的销售数据
sqldf("select * from sale where year =2012")

year market sale profit
1 2012 东 31619 2106
2 2012 南 32443 3124
3 2012 西 32103 2593
4 2012 北 31744 2962

对观测进行排序,使用order by
sqldf("select year,market,sale,profit from sale order by market") #按照市场排序

year market sale profit
1 2010 东 33912 2641
2 2011 东 31651 2437
3 2012 东 31619 2106
4 2010 北 31884 2673
5 2011 北 30555 2749
6 2012 北 31744 2962
7 2010 南 32246 2699
8 2011 南 30572 2853
9 2012 南 32443 3124
10 2010 西 34792 2574
11 2011 西 34175 2877
12 2012 西 32103 2593

2.数据整合

sale[,c('year','market','sale','profit')] #选择字段并展示

year market sale profit
1 2010 东 33912 2641
2 2010 南 32246 2699
3 2010 西 34792 2574
4 2010 北 31884 2673
5 2011 东 31651 2437
6 2011 南 30572 2853
7 2011 西 34175 2877
8 2011 北 30555 2749
9 2012 东 31619 2106
10 2012 南 32443 3124
11 2012 西 32103 2593
12 2012 北 31744 2962

sale$year

[1] 2010 2010 2010 2010 2011 2011 2011 2011 2012 2012 2012 2012

unique(sale$year)

[1] 2010 2011 2012

sale[1:3,] #展示第一到第三行销售数据

year market sale profit
1 2010 东 33912 2641
2 2010 南 32246 2699
3 2010 西 34792 2574

sale[c(1,3),] #展示第一、第三行,两行的销售数据

year market sale profit
1 2010 东 33912 2641
3 2010 西 34792 2574

sale[c(1,2,3),c('year','profit')] #选择行和列

year profit
1 2010 2641
2 2010 2699
3 2010 2574

sale[sale$year==2012,c('year','profit')]#一个条件的筛选

year profit
9 2012 2106
10 2012 3124
11 2012 2593
12 2012 2962

sale[saleyear==2012&saleprofit>2500,]#多个条件的筛选

year market sale profit
10 2012 南 32443 3124
11 2012 西 32103 2593
12 2012 北 31744 2962

sale[order(sale$profit),] #按照profit排序,默认升序

year market sale profit
9 2012 东 31619 2106
5 2011 东 31651 2437
3 2010 西 34792 2574
11 2012 西 32103 2593
1 2010 东 33912 2641
4 2010 北 31884 2673
2 2010 南 32246 2699
8 2011 北 30555 2749
6 2011 南 30572 2853
7 2011 西 34175 2877
12 2012 北 31744 2962
10 2012 南 32443 3124

sale[order(saleyear,saleprofit,decreasing = T),] #按照year和profit排序,降序

year market sale profit
10 2012 南 32443 3124
12 2012 北 31744 2962
11 2012 西 32103 2593
9 2012 东 31619 2106
7 2011 西 34175 2877
6 2011 南 30572 2853
8 2011 北 30555 2749
5 2011 东 31651 2437
2 2010 南 32246 2699
4 2010 北 31884 2673
1 2010 东 33912 2641
3 2010 西 34792 2574

3.数据纵向合并

one <- read.csv("one.csv")
two1 <- read.csv("two1.csv")

one
x a
1 1 a
2 1 a
3 1 b
4 2 c
5 3 v
6 4 e
7 6 g
two1
x a
1 1 x
2 2 y
3 3 z
4 3 v
5 5 w

rbind(one,two1) #纵向合并》

x a
1 1 a
2 1 a
3 1 b
4 2 c
5 3 v
6 4 e
7 6 g
8 1 x
9 2 y
10 3 z
11 3 v

12 5 w

sqldf("select * from one union select * from two1") #去除重复值

x a
1 1 a
2 1 b
3 1 x
4 2 c
5 2 y
6 3 v
7 3 z
8 4 e
9 5 w
10 6 g

sqldf("select * from one union all select * from two1") #不去除重复数值

x a
1 1 a
2 1 a
3 1 b
4 2 c
5 3 v

6 4 e
7 6 g
8 1 x
9 2 y
10 3 z
11 3 v
12 5 w

sqldf("select * from one except select * from two1") #差集

x a
1 1 a
2 1 b
3 2 c
4 4 e
5 6 g

sqldf("select * from one INTERSECT select * from two1") #交集

x a
1 3 v

4.数据横向合并

数据横向连接包括笛卡尔连接、内连接和外连接,其中外连接又包括:左连接、全连接和又连接
table1
id a
1 1 a
2 2 b
3 3 c
table2
id b
1 4 d
2 3 e
sqldf("select * from table1,table2") #笛卡尔连接

id a id..3 b
1 1 a 4 d
2 1 a 3 e
3 2 b 4 d
4 2 b 3 e
5 3 c 4 d
6 3 c 3 e

inner1 <- merge(table1,table2,by = "id",all=FALSE) #内连接

id a b
1 3 c e

inner2 <- sqldf("select * from table1 as a inner join table2 as b on a.id=b.id")

id a id..3 b
1 3 c 3 e

left1 <- merge(table1,table2,by = "id",all.x = TRUE) #左连接

id a b
1 1 a <NA>
2 2 b <NA>
3 3 c e

left3 <- sqldf("select * from table1 as a left join table2 as b on a.id =b.id")

id a id..3 b
1 1 a NA <NA>
2 2 b NA <NA>
3 3 c 3 e

right1 <- merge(table1,table2,by = "id",all.y = TRUE) #右连接

id a b
1 3 c e
2 4 <NA> d

full1<- merge(table1,table2,by ="id",all=TRUE) #全连接

id a b
1 1 a <NA>
2 2 b <NA>
3 3 c e
4 4 <NA> d

相关文章

  • 数据整合和数据清洗

    1.数据整合 使用SQL语句,structured query language 结构化查询语言,是一种面向对象的...

  • 数据整合2018-12-26

    什么是数据整合? 数据整合就是把不同数据源的数据收集、整理、清洗、转换后加载都一个新的数据源,是为了给用户提供统一...

  • MapReduce将HDFS数据清洗到多个Hbase表中

    最近一直在对历史数据进行清洗,原始数据是纯数据格式,现在要清洗到hbase中,方便后期跟hive进行整合查询。。可...

  • ETL过程的数据清洗和整合

    子系统五:错误事件处理系统 主要目的是记录ETL流水线过程中所有质量单元出现的错误时间。也可用于其他应用之间传输数...

  • 数据处理|R-dplyr

    dplyr包实现数据的清洗处理,包括数据整合、关联、排序、筛选、汇总、分组等。 1)安装、加载dplyr包、准备数...

  • Kettle入门之一 介绍、安装

    数据整合是吧在不同数据源的数据收集、整理、清洗、转换(有点像ETL)后,加载到一个新的数据源,为数据使用者提供统一...

  • 【工具安装和配置】Kettle 安装和配置

    数据整合是吧在不同数据源的数据收集、整理、清洗、转换(有点像ETL)后,加载到一个新的数据源,为数据使用者提供统一...

  • 第三章-数据预处理

    数据预处理的主要内容包括数据清洗、数据集成、数据变换和数据规约。 3.1数据清洗 数据清洗主要是删除原始数据集中的...

  • 缺失值处理1

    数据预处理步骤有数据清洗、数据集成、数据变换、数据规约。实际工作中不是每一步都必须。 数据清洗包括缺失值和异常值,...

  • 自动化测试+爬虫+数据可视化 (2)爬虫部分

    一、前言 其实在selenium中已经整合了部分爬虫,这里我单独拧出来讲。 二、数据清洗与爬虫处理 在得到数据之后...

网友评论

      本文标题:数据整合和数据清洗

      本文链接:https://www.haomeiwen.com/subject/zmjchqtx.html