数据整合和数据清洗

2018-12-08  本文已影响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

上一篇下一篇

猜你喜欢

热点阅读