首页 > > > 【R语言基础】06. 数据整合Ⅱ
1. 使用函数
rbind(),增加行(样本数据),要求宽度(列数)相同;
cbind(),增加列(属性变量),要求高度(行数)相同
2. 使用函数merge()
用来合并两个数据框,按行名或变量名匹配。基本格式为:
merge(x, y, by=…, by.x=…, by.y =…, all=…,
all.x=…, all.y=…, ...)
其中,
x, y为要合并的数据集;
by指定合并的依据(相同的行或列),默认按相同的列连接;
by.x, by.y分别为第一、二个数据框指定连接的列;
all, all.x, all.y默认为FALSE,仅输出x和y中都包含的行(内连接);all.x为TURE,则为左外连接;all.y为TURE,则为右外连接;all=TRUE,则为全连接。
仅返回两个表匹配的数据

左外连接:返回左表的数据,以及匹配的数据
右外连接:返回右表的数据,以及匹配的数据
全连接:返回左右表的数据,以及匹配的数据
例1
setwd("E:/办公资料/R语言/R语言学习系列/codes")
load("salary.Rdata")
index<-list("City"=data$City,"Index"=1:15)
data1<-merge(data, index, by="City")
head(data1)
City Work Price Salary Index
1 Amsterdam 1714 65.6 49.0 1
2 Bombay 2052 30.3 5.3 2
3 Chicago 1924 73.9 61.9 3
4 Dublin 1759 76.0 41.4 4
5 Frankfurt 1650 74.5 60.4 5
6 London 1737 84.2 46.2 6
例2
authors <- data.frame(
surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
nationality = c("US", "Australia", "US", "UK", "Australia"),
deceased = c("yes", rep("no", 4)))
authors
surname nationality deceased
1 Tukey US yes
2 Venables Australia no
3 Tierney US no
4 Ripley UK no
5 McNeil Australia no
books <- data.frame(
name = I(c("Tukey", "Venables", "Tierney",
"Ripley", "Ripley", "McNeil", "R Core")),
title = c("Exploratory Data Analysis",
"Modern Applied Statistics ...",
"LISP-STAT",
"Spatial Statistics", "Stochastic Simulation",
"Interactive Data Analysis",
"An Introduction to R"),
other.author = c(NA, "Ripley", NA, NA, NA, NA,
"Venables & Smith"))
books
name title other.author
1 Tukey Exploratory Data Analysis <NA>
2 Venables Modern Applied Statistics ... Ripley
3 Tierney LISP-STAT <NA>
4 Ripley Spatial Statistics <NA>
5 Ripley Stochastic Simulation <NA>
6 McNeil Interactive Data Analysis <NA>
7 R Core An Introduction to R Venables & Smith
m1 <- merge(authors, books, by.x = "surname", by.y = "name") #内连接
m1
surname nationality deceased title other.author
1 McNeil Australia no Interactive Data Analysis <NA>
2 Ripley UK no Spatial Statistics <NA>
3 Ripley UK no Stochastic Simulation <NA>
4 Tierney US no LISP-STAT <NA>
5 Tukey US yes Exploratory Data Analysis <NA>
6 Venables Australia no Modern Applied Statistics ... Ripley
m2 <- merge(authors, books, by.x = "surname", by.y = "name",all.x=TRUE) #左外连接
m2
surname nationality deceased title other.author
1 McNeil Australia no Interactive Data Analysis <NA>
2 Ripley UK no Spatial Statistics <NA>
3 Ripley UK no Stochastic Simulation <NA>
4 Tierney US no LISP-STAT <NA>
5 Tukey US yes Exploratory Data Analysis <NA>
6 Venables Australia no Modern Applied Statistics ... Ripley
m3 <- merge(authors, books, by.x = "surname", by.y = "name",all.y=TRUE) #右外连接
m3
surname nationality deceased title other.author
1 McNeil Australia no Interactive Data Analysis <NA>
2 R Core <NA> <NA> An Introduction to R Venables & Smith
3 Ripley UK no Spatial Statistics <NA>
4 Ripley UK no Stochastic Simulation <NA>
5 Tierney US no LISP-STAT <NA>
6 Tukey US yes Exploratory Data Analysis <NA>
7 Venables Australia no Modern Applied Statistics ... Ripley
m4 <- merge(authors, books, by.x = "surname", by.y = "name",all=TRUE) #全连接
m4
surname nationality deceased title other.author
1 McNeil Australia no Interactive Data Analysis <NA>
2 R Core <NA> <NA> An Introduction to R Venables & Smith
3 Ripley UK no Spatial Statistics <NA>
4 Ripley UK no Stochastic Simulation <NA>
5 Tierney US no LISP-STAT <NA>
6 Tukey US yes Exploratory Data Analysis <NA>
7 Venables Australia no Modern Applied Statistics ... Ripley
x <- data.frame(k1 = c(NA,NA,3,4,5), k2 = c(1,NA,NA,4,5), data = 1:5)
y <- data.frame(k1 = c(NA,2,NA,4,5), k2 = c(NA,NA,3,4,5), data = 1:5)
x
k1 k2 data
1 NA 1 1
2 NA NA 2
3 3 NA 3
4 4 4 4
5 5 5 5
y
k1 k2 data
1 NA NA 1
2 2 NA 2
3 NA 3 3
4 4 4 4
5 5 5 5
merge(x, y, by = c("k1","k2")) #多变量匹配的内连接
k1 k2 data.x data.y
1 4 4 4 4
2 5 5 5 5
3 NA NA 2 1 有时数据需要拆分,尤其是数据量较大时,可能需要提出其中一部分做分析,这就需要对变量(列)和观测(行)进行选入和排除。
1. 通过行标、列标或索引
另外,行列标中可以直接使用表达式或which语句,例如
Newdata<-data[data$Salary>6500, ]
newdata<-data[which(data$Salary>6500 & age > 30), ]
注:排除用“-”号。
从数据框中选择变量,例如
newdata<-data["x1","x2","x3"]
剔除变量:
vars<-names(data) %in% c("x2","x3")
newdata<-data[!vars]
2. 函数subset()——选取子集
基本格式为:
subset(x, subset, select, ...)
其中,
x为数据对象;
subset为逻辑表达式,用来指定选取满足某条件的样本;
select指定选取的变量(列)
例如,
newdata<-subset(data, age>=35 | age <24,
select=c(q1, q2, q3))
3. 使用SQL语句操作数据框
使用sqldf包中的函数sqldf()
library(sqldf)
#从mtcars选择所有变量,只选使用化油器carb的车型(行),按mpg升序排序
newdf<-sqldf("select * from mtcars where carb=1 order by mpg", row.names=TRUE)
newdf
mpg cyl disp hp drat wt qsec vs am gear carb
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#输出四缸车和六缸车每一gear水平的mpg和disp的平均值
sqldf("select avg(mpg) as avg_mpg, avg(disp) as avg_disp, gear from mtcars where cyl in (4,6) group by gear")
avg_mpg avg_disp gear
1 20.33333 201.0333 3
2 24.53333 123.0167 4
3 25.36667 120.1333 5
用少量数据测试数据集时,常用随机抽样方法从整体中选出部分样本数据。
简单随机抽样,是指从总体N个样本中任意抽取n个样本,每个样本被抽中的概率相等;分为重复抽样(有放回)、不重复抽样(不放回)。
使用sampling包实现。
1. 有放回简单随机抽样
函数srswr(),基本格式为:
srswr(n, N)
表示从总体N中有放回地随机抽取n个样本,返回一个长度为N的向量,每个分量分别表示各元素被抽取到的次数。
library(sampling)
LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K"
[12] "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V"
[23] "W" "X" "Y" "Z"
s<-srswr(10,26)
s
[1] 2 0 1 1 0 0 0 0 1 0 0 2 0 0 0 3 0 0 0 0 0 0 0
[24] 0 0 0
ind<-(1:26)[s!=0] #被抽到的样本编号
ind
[1] 1 3 4 9 12 16
n<-s[s!=0] #被抽到的样本的被抽到的次数
n
[1] 2 1 1 1 2 3
ind<-rep(ind,times=n) #按次数重复被抽到的样本编号
ind
[1] 1 1 3 4 9 12 12 16 16 16
sample<-LETTERS[ind] #被抽到的字母
sample
[1] "A" "A" "C" "D" "I" "L" "L" "P" "P" "P"
2. 不放回简单随机抽样
函数srswor(),格式和返回值同srswr(),注意返回值向量中只有0和1.
s<-srswor(10,26)
s
[1] 1 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 1 0 0 1
[24] 1 0 1
ind<-(1:26)[s!=0]
ind
[1] 1 6 8 11 18 19 20 23 24 26
sample<-LETTERS[ind]
sample
[1] "A" "F" "H" "K" "R" "S" "T" "W" "X" "Z"
3. 函数simple()
实现有放回和不放回的简单随机抽样,基本格式为:
sample(x, size, replace = FALSE)其中,
x为数据集;
size为抽取样本数;
replace指定是否放回,默认为FALSE(不放回),TURE为有放回
也可对数据进行随机分组:
sample(num, size, replace = TRUE, prob = NULL)
其中,
num为分组数;
size为抽取样本数;
replace必须为TRUE;
prob为权重向量(分组比例)
sample(LETTERS,10,replace=TRUE) #有放回简单随机抽样
[1] "K" "C" "E" "J" "Z" "C" "W" "E" "G" "B"
sample(LETTERS,10,replace=FALSE) #无放回简单随机抽样
[1] "I" "S" "T" "A" "D" "Q" "R" "X" "J" "P"
#将26个字母随机分为两组,比例是0.7:0.3
n<-sample(2,26,replace=TRUE,prob=c(0.7,0.3))
n
[1] 1 1 1 2 2 1 1 1 2 1 1 2 2 1 2 1 1 1 1 1 1 2 2
[24] 1 1 1
sample1<-LETTERS[n==1] #第1组
sample1
[1] "A" "B" "C" "F" "G" "H" "J" "K" "N" "P" "Q"
[12] "R" "S" "T" "U" "X" "Y" "Z"
sample2<-LETTERS[n==2] #第2组
sample2
[1] "D" "E" "I" "L" "M" "O" "V" "W"
[1] Robert I. Kabacoff著,高涛,肖楠,陈钢 译,R语言实战. 人民邮电出版社.
[2] 李诗羽,张飞,王正林,数据分析:R语言实战. 电子工业出版社.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3119373/ ,如需转载,请注明出处,否则将追究法律责任。