📅  最后修改于: 2021-01-08 09:44:35             🧑  作者: Mango
数据帧是二维数组状结构或表,其中一列包含一个变量的值,行包含每一列的一组值。数据帧是列表的一种特殊情况,其中每个组件的长度相等。
数据帧用于存储数据表,并且在数据帧中以列表形式存在的向量长度相等。
以简单的方式,它是等长向量的列表。矩阵可以包含一种类型的数据,但是数据框可以包含不同的数据类型,例如数字,字符,因子等。
数据帧具有以下特征。
在R中,借助数据的frame()函数创建数据帧。此函数包含任何类型的向量,例如数字,字符或整数。在下面的示例中,我们创建一个数据框,其中包含员工ID(整数向量),员工姓名(字符向量),薪水(数字向量)和开始日期(日期向量)。
例
# Creating the data frame.
emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,915.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Printing the data frame.
print(emp.data)
输出量
employee_idemployee_namesalstarting_date
1 1 Shubham623.30 2012-01-01
2 2 Arpita915.20 2013-09-23
3 3 Nishka611.00 2014-11-15
4 4 Gunjan729.00 2014-05-11
5 5 Sumit843.25 2015-03-27
在R中,我们可以找到数据帧的结构。 R提供了一个称为str()的内置函数,该函数返回具有完整结构的数据。在下面的示例中,我们使用不同数据类型的向量创建了一个框架,并提取了其结构。
例
# Creating the data frame.
emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,515.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Printing the structure of data frame.
str(emp.data)
输出量
'data.frame': 5 obs. of 4 variables:
$ employee_id : int 1 2 3 4 5
$ employee_name: chr "Shubham" "Arpita" "Nishka" "Gunjan" ...
$ sal : num 623 515 611 729 843
$ starting_date: Date, format: "2012-01-01" "2013-09-23" ...
数据帧的数据对我们来说至关重要。为了操纵数据帧的数据,必须从数据帧中提取数据。我们可以通过以下三种方式提取数据:
让我们看一下每个示例,以了解如何借助这些方法从数据框中提取数据。
例
# Creating the data frame.
emp.data<- data.frame(
employee_id = c (1:5),
employee_name= c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,515.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Extracting specific columns from a data frame
final <- data.frame(emp.data$employee_id,emp.data$sal)
print(final)
输出量
emp.data.employee_idemp.data.sal
1 1 623.30
2 2 515.20
3 3 611.00
4 4 729.00
5 5 843.25
例
# Creating the data frame.
emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,515.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Extracting first row from a data frame
final <- emp.data[1,]
print(final)
# Extracting last two row from a data frame
final <- emp.data[4:5,]
print(final)
输出量
employee_id employee_name sal starting_date
1 1 Shubham 623.3 2012-01-01
employee_id employee_name sal starting_date
4 4 Gunjan 729.00 2014-05-11
5 5 Sumit 843.25 2015-03-27
例
# Creating the data frame.
emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,515.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Extracting 2nd and 3rd row corresponding to the 1st and 4th column
final <- emp.data[c(2,3),c(1,4)]
print(final)
输出量
employee_id starting_date
2 2 2013-09-23
3 3 2014-11-15
R允许我们在数据框中进行修改。像矩阵修改一样,我们可以通过重新分配来修改数据框。我们不仅可以添加行和列,还可以删除它们。通过添加行和列来扩展数据框。
我们可以
让我们看一个示例,以了解rbind()函数的工作方式以及如何在数据帧中进行修改。
示例:添加行和列
# Creating the data frame.
emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,515.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
print(emp.data)
#Adding row in the data frame
x <- list(6,"Vaishali",547,"2015-09-01")
rbind(emp.data,x)
#Adding column in the data frame
y <- c("Moradabad","Lucknow","Etah","Sambhal","Khurja")
cbind(emp.data,Address=y)
输出量
employee_id employee_name sal starting_date
1 1 Shubham 623.30 2012-01-01
2 2 Arpita 515.20 2013-09-23
3 3 Nishka 611.00 2014-11-15
4 4 Gunjan 729.00 2014-05-11
5 5 Sumit 843.25 2015-03-27
employee_id employee_name sal starting_date
1 1 Shubham 623.30 2012-01-01
2 2 Arpita 515.20 2013-09-23
3 3 Nishka 611.00 2014-11-15
4 4 Gunjan 729.00 2014-05-11
5 5 Sumit 843.25 2015-03-27
6 6 Vaishali 547.00 2015-09-01
employee_id employee_name sal starting_date Address
1 1 Shubham 623.30 2012-01-01 Moradabad
2 2 Arpita 515.20 2013-09-23 Lucknow
3 3 Nishka 611.00 2014-11-15 Etah
4 4 Gunjan 729.00 2014-05-11 Sambhal
5 5 Sumit 843.25 2015-03-27 Khurja
示例:删除行和列
# Creating the data frame.
emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,515.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
print(emp.data)
#Delete rows from data frame
emp.data<-emp.data[-1,]
print(emp.data)
#Delete column from the data frame
emp.data$starting_date<-NULL
print(emp.data)
输出量
employee_idemployee_namesalstarting_date
1 1 Shubham623.30 2012-01-01
2 2 Arpita515.20 2013-09-23
3 3 Nishka611.00 2014-11-15
4 4 Gunjan729.00 2014-05-11
5 5 Sumit843.25 2015-03-27
employee_idemployee_namesalstarting_date
2 2 Arpita515.20 2013-09-23
3 3 Nishka611.00 2014-11-15
4 4 Gunjan729.00 2014-05-11
5 5 Sumit843.25 2015-03-27
employee_idemployee_namesal
1 1 Shubham623.30
2 2 Arpita515.20
3 3 Nishka611.00
4 4 Gunjan729.00
5 5 Sumit843.25
在某些情况下,需要在数据框中找到统计摘要和数据的性质。 R提供了summary()函数来提取统计摘要和数据的性质。该函数以数据帧为参数,并返回数据的统计信息。让我们看一个示例,以了解如何在R中使用此函数:
例
# Creating the data frame.
emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,515.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
print(emp.data)
#Printing the summary
print(summary(emp.data))
输出量
employee_idemployee_namesalstarting_date
1 1 Shubham623.30 2012-01-01
2 2 Arpita515.20 2013-09-23
3 3 Nishka611.00 2014-11-15
4 4 Gunjan729.00 2014-05-11
5 5 Sumit843.25 2015-03-27
employee_idemployee_namesalstarting_date
Min. :1 Length:5 Min. :515.2 Min. :2012-01-01
1st Qu.:2 Class :character 1st Qu.:611.0 1st Qu.:2013-09-23
Median :3 Mode :character Median :623.3 Median :2014-05-11
Mean :3 Mean :664.4 Mean :2014-01-14
3rd Qu.:4 3rd Qu.:729.0 3rd Qu.:2014-11-15
Max. :5 Max. :843.2 Max. :2015-03-27