Copyright © 2022-2024 aizws.net · 网站版本: v1.2.6·内部版本: v1.23.3·
页面加载耗时 0.00 毫秒·物理内存 61.9MB ·虚拟内存 1299.8MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
R 语言合并数据框使用 merge() 函数。
merge() 函数语法格式如下:
# S3 方法 merge(x, y, …) # data.frame 的 S3 方法 merge(x, y, by = intersect(names(x), names(y)), by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all, sort = TRUE, suffixes = c(".x",".y"), no.dups = TRUE, incomparables = NULL, …)
常用参数说明:
merge() 函数和 SQL 的 JOIN 功能很相似:
# data frame 1 df1 = data.frame(SiteId = c(1:6), Site = c("Google","Aizws","Taobao","Facebook","Zhihu","Weibo")) # data frame 2 df2 = data.frame(SiteId = c(2, 4, 6, 7, 8), Country = c("CN","USA","CN","USA","IN")) # INNER JOIN df1 = merge(x=df1,y=df2,by="SiteId") print("----- INNER JOIN -----") print(df1) # FULL JOIN df2 = merge(x=df1,y=df2,by="SiteId",all=TRUE) print("----- FULL JOIN -----") print(df2) # LEFT JOIN df3 = merge(x=df1,y=df2,by="SiteId",all.x=TRUE) print("----- LEFT JOIN -----") print(df3) # RIGHT JOIN df4 = merge(x=df1,y=df2,by="SiteId",all.y=TRUE) print("----- RIGHT JOIN -----") print(df4)
执行以上代码输出结果为:
[1] "----- INNER JOIN -----" SiteId Site Country 1 2 Aizws CN 2 4 Facebook USA 3 6 Weibo CN [1] "----- FULL JOIN -----" SiteId Site Country.x Country.y 1 2 Aizws CN CN 2 4 Facebook USA USA 3 6 Weibo CN CN 4 7 <NA> <NA> USA 5 8 <NA> <NA> IN [1] "----- LEFT JOIN -----" SiteId Site.x Country Site.y Country.x Country.y 1 2 Aizws CN Aizws CN CN 2 4 Facebook USA Facebook USA USA 3 6 Weibo CN Weibo CN CN [1] "----- RIGHT JOIN -----" SiteId Site.x Country Site.y Country.x Country.y 1 2 Aizws CN Aizws CN CN 2 4 Facebook USA Facebook USA USA 3 6 Weibo CN Weibo CN CN 4 7 <NA> <NA> <NA> <NA> USA 5 8 <NA> <NA> <NA> <NA> IN
R 语言使用 melt() 和 cast() 函数来对数据进行整合和拆分。
下图很好展示来 melt() 和 cast() 函数的功能(后面范例会详细说明):
melt() 将数据集的每个列堆叠到一个列中,函数语法格式:
melt(data, ..., na.rm = FALSE, value.name = "value")
参数说明:
进行以下操作之前,我们先安装依赖包:
# 安装库,MASS 包含很多统计相关的函数,工具和数据集 install.packages("MASS", repos = "https://mirrors.ustc.edu.cn/CRAN/") # melt() 和 cast() 函数需要对库 install.packages("reshape2", repos = "https://mirrors.ustc.edu.cn/CRAN/") install.packages("reshape", repos = "https://mirrors.ustc.edu.cn/CRAN/")
测试范例:
# 载入库 library(MASS) library(reshape2) library(reshape) # 创建数据框 id<- c(1, 1, 2, 2) time <- c(1, 2, 1, 2) x1 <- c(5, 3, 6, 2) x2 <- c(6, 5, 1, 4) mydata <- data.frame(id, time, x1, x2) # 原始数据框 cat("原始数据框:\n") print(mydata) # 整合 md <- melt(mydata, id = c("id","time")) cat("\n整合后:\n") print(md)
执行以上代码输出结果为:
原始数据框: id time x1 x2 1 1 1 5 6 2 1 2 3 5 3 2 1 6 1 4 2 2 2 4 整合后: id time variable value 1 1 1 x1 5 2 1 2 x1 3 3 2 1 x1 6 4 2 2 x1 2 5 1 1 x2 6 6 1 2 x2 5 7 2 1 x2 1 8 2 2 x2 4
cast 函数用于对合并对数据框进行还原,dcast() 返回数据框,acast() 返回一个向量/矩阵/数组。
cast() 函数语法格式:
dcast( data, formula, fun.aggregate = NULL, ..., margins = NULL, subset = NULL, fill = NULL, drop = TRUE, value.var = guess_value(data) ) acast( data, formula, fun.aggregate = NULL, ..., margins = NULL, subset = NULL, fill = NULL, drop = TRUE, value.var = guess_value(data) )
参数说明:
# 载入库 library(MASS) library(reshape2) library(reshape) # 创建数据框 id<- c(1, 1, 2, 2) time <- c(1, 2, 1, 2) x1 <- c(5, 3, 6, 2) x2 <- c(6, 5, 1, 4) mydata <- data.frame(id, time, x1, x2) # 整合 md <- melt(mydata, id = c("id","time")) # Print recasted dataset using cast() function cast.data <- cast(md, id~variable, mean) print(cast.data) cat("\n") time.cast <- cast(md, time~variable, mean) print(time.cast) cat("\n") id.time <- cast(md, id~time, mean) print(id.time) cat("\n") id.time.cast <- cast(md, id+time~variable) print(id.time.cast) cat("\n") id.variable.time <- cast(md, id+variable~time) print(id.variable.time) cat("\n") id.variable.time2 <- cast(md, id~variable+time) print(id.variable.time2)
执行以上代码输出结果为:
id x1 x2 1 1 4 5.5 2 2 4 2.5 time x1 x2 1 1 5.5 3.5 2 2 2.5 4.5 id 1 2 1 1 5.5 4 2 2 3.5 3 id time x1 x2 1 1 1 5 6 2 1 2 3 5 3 2 1 6 1 4 2 2 2 4 id variable 1 2 1 1 x1 5 3 2 1 x2 6 5 3 2 x1 6 2 4 2 x2 1 4 id x1_1 x1_2 x2_1 x2_2 1 1 5 3 6 5 2 2 6 2 1 4