R语言笔记——合并表
本个专题将介绍R语言的一些基本技能和实用技巧。本教程说明了如何在R中联接(合并)两个表。
创建两个表
表一:DF1
> mydata320 = mydata[,grepl("*C_A",names(mydata))]
> df1 <- data.frame(ID = c(1, 2, 3, 4, 5),
+ w = c('a', 'b', 'c', 'd', 'e'),
+ x = c(1, 1, 0, 0, 1),
+ y=rnorm(5),
+ z=letters[1:5])
# ID w x y z
# 1 1 a 1 -0.9158105 a
# 2 2 b 1 1.3110974 b
# 3 3 c 0 0.9887263 c
# 4 4 d 0 1.6539287 d
# 5 5 e 1 -1.4408052 e
表二:DF2
> df2 <- data.frame(ID = c(1, 7, 3, 6, 8),
+ a = c('z', 'b', 'k', 'd', 'l'),
+ b = c(1, 2, 3, 0, 4),
+ c =rnorm(5),
+ d =letters[2:6])
"okay", "fine", "She is okay.") sub(
# ID a b c d
# 1 1 z 1 1.9473564 b
# 2 7 b 2 1.7369362 c
# 3 3 k 3 0.3874833 d
# 4 6 d 0 2.2800340 e
# 5 8 l 4 1.5378833 f
内部联接
"ID") df3 = merge(df1, df2, by =
如果两个表(数据帧)中的主键(匹配变量)名称都不相同
> df3 = merge(df1, df2, by.x ="ID", by.y="ID")
左加入
"ID", all.x = TRUE) df4 = merge(df1, df2, by =
# ID w x y z a b c d
# 1 1 a 1 -0.9158105 a z 1 1.9473564 b
# 2 2 b 1 1.3110974 b NA NA NA NA
# 3 3 c 0 0.9887263 c k 3 0.3874833 d
# 4 4 d 0 1.6539287 d NA NA NA NA
# 5 5 e 1 -1.4408052 e NA NA NA NA
右加入
"ID", all.y = TRUE) df4 = merge(df1, df2, by =
# ID w x y z a b c d
# 1 1 a 1 -0.9158105 a z 1 1.9473564 b
# 2 3 c 0 0.9887263 c k 3 0.3874833 d
# 3 6 NA NA NA NA d 0 2.2800340 e
# 4 7 NA NA NA NA b 2 1.7369362 c
# 5 8 NA NA NA NA l 4 1.5378833 f
完全(外部)加入
"ID", all = TRUE) df6 = merge(df1, df2, by =
交叉连接
df7 = merge(df1, df2, by = NULL)
◆
◆
◆
◆