菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

R 数据框


数据框

数据框是以表格形式显示的数据。

数据框可以包含不同类型的数据。虽然第一列可以是 character(字符型),但第二和第三列可以是 numeric(数值型)或 logical(逻辑型)。但是,每一列都应该包含相同类型的数据。

使用 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
自己动手试一试 »
×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持