小白教程
所有教程
关于
Search
172.69.7.22
172.69.7.22
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“R 数据框”的源代码
本页内容
上一节:
R_因子
下一节:
R_数据重塑
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:R 数据框}}[[Category:R 教程|15]] = R 数据框 = 数据框(Data frame)可以理解成我们常说的"表格"。 数据框是 R 语言的数据结构,是特殊的二维列表。 数据框每一列都有一个唯一的列名,长度都是相等的,同一列的数据类型需要一致,不同列的数据类型可以不一样。 R 语言数据框使用 data.frame() 函数来创建,语法格式如下: <sample title="" desc="" lang="r" hererun="1"> data.frame(…, row.names = NULL, check.rows = FALSE, check.names = TRUE, fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors()) </sample> * '''…''': 列向量,可以是任何类型(字符型、数值型、逻辑型),一般以 tag = value 的形式表示,也可以是 value。 * '''row.names''': 行名,默认为 NULL,可以设置为单个数字、字符串或字符串和数字的向量。 * '''check.rows''': 检测行的名称和长度是否一致。 * '''check.names''': 检测数据框的变量名是否合法。 * '''fix.empty.names''': 设置未命名的参数是否自动设置名字。 * '''stringsAsFactors''': 布尔值,字符是否转换为因子,factory-fresh 的默认值是 TRUE,可以通过设置选项(stringsAsFactors=FALSE)来修改。 以下创建一个简单的数据框,包含姓名、工号、月薪: <sample title="" desc="" lang="r" hererun="1"> table = data.frame( 姓名 = c("张三", "李四"), 工号 = c("001","002"), 月薪 = c(1000, 2000) ) print(table) # 查看 table 数据 </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> 姓名 工号 月薪 1 张三 001 1000 2 李四 002 2000 </sample> 数据框的数据结构可以通过 '''str()''' 函数来展示: <sample title="" desc="" lang="r" hererun="1"> table = data.frame( 姓名 = c("张三", "李四"), 工号 = c("001","002"), 月薪 = c(1000, 2000) ) # 获取数据结构 str(table) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> 'data.frame': 2 obs. of 3 variables: $ 姓名: chr "张三" "李四" $ 工号: chr "001" "002" $ 月薪: num 1000 2000 </sample> '''summary()''' 可以显示数据框的概要信息: <sample title="" desc="" lang="r" hererun="1"> table = data.frame( 姓名 = c("张三", "李四"), 工号 = c("001","002"), 月薪 = c(1000, 2000) ) # 显示概要 print(summary(table)) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> 姓名 工号 月薪 Length:2 Length:2 Min. :1000 Class :character Class :character 1st Qu.:1250 Mode :character Mode :character Median :1500 Mean :1500 3rd Qu.:1750 Max. :2000 </sample> 我们也可以提取指定的列: <sample title="" desc="" lang="r" hererun="1"> table = data.frame( 姓名 = c("张三", "李四"), 工号 = c("001","002"), 月薪 = c(1000, 2000) ) # 提取指定的列 result <- data.frame(table$姓名,table$月薪) print(result) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> table.姓名 table.月薪 1 张三 1000 2 李四 2000 </sample> 以下形式显示前面两行: <sample title="" desc="" lang="r" hererun="1"> table = data.frame( 姓名 = c("张三", "李四","王五"), 工号 = c("001","002","003"), 月薪 = c(1000, 2000,3000) ) print(table) # 提取前面两行 print("---输出前面两行----") result <- table[1:2,] print(result) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> 姓名 工号 月薪 1 张三 001 1000 2 李四 002 2000 3 王五 003 3000 [1] "---输出前面两行----" 姓名 工号 月薪 1 张三 001 1000 2 李四 002 2000 </sample> 我们可以通过类似坐标的形式读取指定行的某一列的数据,以下我们读取第 2 、3 行的第 1 、2 列数据:: <sample title="" desc="" lang="r" hererun="1"> table = data.frame( 姓名 = c("张三", "李四","王五"), 工号 = c("001","002","003"), 月薪 = c(1000, 2000,3000) ) # 读取第 2 、3 行的第 1 、2 列数据: result <- table[c(2,3),c(1,2)] print(result) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> 姓名 工号 2 李四 002 3 王五 003 </sample> === 扩展数据框 === 我们可以对已有的数据框进行扩展,以下实例我们添加部门列: <sample title="" desc="" lang="r" hererun="1"> table = data.frame( 姓名 = c("张三", "李四","王五"), 工号 = c("001","002","003"), 月薪 = c(1000, 2000,3000) ) # 添加部门列 table$部门 <- c("运营","技术","编辑") print(table) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> 姓名 工号 月薪 部门 1 张三 001 1000 运营 2 李四 002 2000 技术 3 王五 003 3000 编辑 </sample> 我们可以使用 '''cbind()''' 函数将多个向量合成一个数据框: <sample title="" desc="" lang="r" hererun="1"> # 创建向量 sites <- c("Google","xiaoBai","Taobao") likes <- c(222,111,123) url <- c("www.google.com","www.xiaobai.wang","www.taobao.com") # 将向量组合成数据框 addresses <- cbind(sites,likes,url) # 查看数据框 print(addresses) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> sites likes url [1,] "Google" "222" "www.google.com" [2,] "xiaoBai" "111" "www.xiaobai.wang" [3,] "Taobao" "123" "www.taobao.com" </sample> 如果要对两个数据框进行合并可以使用 '''rbind()''' 函数: <sample title="" desc="" lang="r" hererun="1"> table = data.frame( 姓名 = c("张三", "李四","王五"), 工号 = c("001","002","003"), 月薪 = c(1000, 2000,3000) ) newtable = data.frame( 姓名 = c("小明", "小白"), 工号 = c("101","102"), 月薪 = c(5000, 7000) ) # 合并两个数据框 result <- rbind(table,newtable) print(result) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> 姓名 工号 月薪 1 张三 001 1000 2 李四 002 2000 3 王五 003 3000 4 小明 101 5000 5 小白 102 7000 </sample>
返回至“
R 数据框
”。
上一节:
R_因子
下一节:
R_数据重塑