R 教程

浏览4611
更新时间2025-08-13

数据框

数据框(Data Frames)是以表格格式显示的数据。

数据框内可以包含不同类型的数据。虽然第一列可以是字符,但第二列和第三列可以是数字或逻辑值。但是,每一列应该具有相同类型的数据。

使用 data.frame() 函数创建数据框:

实例

# 创建数据框
Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

# 打印数据框
Data_Frame

总结数据

使用 summary() 函数汇总数据框中的数据:

实例

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

Data_Frame

summary(Data_Frame)

提示:您将在 R 教程的统计部分了解有关 summary() 函数的更多信息。

访问项目

我们可以使用单括号 [ ]、双括号 [[ ]]$ 来访问数据框中的列:

实例

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

Data_Frame[1]

Data_Frame[["Training"]]

Data_Frame$Training

添加行

使用 rbind() 函数在数据框中添加新行:

实例

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

# 添加新行
New_row_DF <- rbind(Data_Frame, c("Strength", 110, 110))

# 打印新行
New_row_DF

添加列

使用 cbind() 函数在数据框中添加新列:

实例

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

# 添加新列
New_col_DF <- cbind(Data_Frame, Steps = c(1000, 6000, 2000))

# 打印新列
New_col_DF

删除行和列

使用 c() 函数删除数据框中的行和列:

实例

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

# 删除第一行和列
Data_Frame_New <- Data_Frame[-c(1), -c(1)]

# 打印新的数据框
Data_Frame_New

行数和列数

使用 dim() 函数查找数据框中的行数和列数:

实例

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

dim(Data_Frame)

您还可以使用 ncol() 函数查找列数,使用 nrow() 函数查找行数:

实例

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

ncol(Data_Frame)
nrow(Data_Frame)

数据框长度

使用 length() 函数查找数据框中的列数(类似于 ncol()):

实例

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

length(Data_Frame)

组合数据框

使用 rbind() 函数垂直组合 R 中的两个或多个数据框:

实例

Data_Frame1 <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

Data_Frame2 <- data.frame (
  Training = c("Stamina", "Stamina", "Strength"),
  Pulse = c(140, 150, 160),
  Duration = c(30, 30, 20)
)

New_Data_Frame <- rbind(Data_Frame1, Data_Frame2)
New_Data_Frame

并使用 cbind() 函数水平组合 R 中的两个或多个数据框:

实例

Data_Frame3 <- data.frame (
  Training = c("Strength", "Stamina", "Other"),
  Pulse = c(100, 150, 120),
  Duration = c(60, 30, 45)
)

Data_Frame4 <- data.frame (
  Steps = c(3000, 6000, 2000),
  Calories = c(300, 400, 300)
)

New_Data_Frame1 <- cbind(Data_Frame3, Data_Frame4)
New_Data_Frame1

相关视频

更多

免费

phpStudy极速入门视频教程
初级phpStudy极速入门视频教程

535507次学习

收藏

免费

Midjourney基础课程
初级Midjourney基础课程

13301次学习

收藏

免费

极客学院Git使用视频教程
初级极客学院Git使用视频教程

48272次学习

收藏

免费

尚观shell视频教程
高级尚观shell视频教程

16824次学习

收藏

免费

尚观Linux入门视频教程
初级尚观Linux入门视频教程

46541次学习

收藏
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号