📜  R – 数据帧

📅  最后修改于: 2022-05-13 01:55:16.792000             🧑  作者: Mango

R – 数据帧

R 编程语言是一种开源编程语言,被广泛用作统计软件和数据分析工具。 R 语言中的数据框是 R的通用数据对象,用于存储表格数据。数据帧也可以解释为矩阵,其中矩阵的每一列都可以是不同的数据类型。 DataFrame 由三个主要组件组成,即数据、行和列。

R – 数据帧

在 R 编程语言中创建数据框

要在 R 中创建数据框,请使用data.frame()命令,然后将您创建的每个向量作为参数传递给函数。

例子:

R
# R program to create dataframe
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
# print the data frame
print(friend.data)


R
# R program to get the
# structure of the data frame
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
# using str()
print(str(friend.data))


R
# R program to get the
# summary of the data frame
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
# using summary()
print(summary(friend.data))


R
# R program to extract
# data from the data frame
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
 
# Extracting friend_name column
result <- data.frame(friend.data$friend_name)
print(result)


R
# R program to expand
# the data frame
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
 
# Expanding data frame
friend.data$location <- c("Kolkata", "Delhi",
                       "Bangalore", "Hyderabad",
                       "Chennai")
resultant <- friend.data
# print the modified data frame
print(resultant)


输出:

friend_id friend_name
1         1      Sachin
2         2      Sourav
3         3      Dravid
4         4      Sehwag
5         5       Dhoni

获取 R – 数据框的结构

可以使用 R 中的str()函数获取数据框的结构。它甚至可以显示嵌套的大型列表的内部结构。它为基本的 R 对象提供单行输出,让用户了解对象及其组成部分。

例子:

R

# R program to get the
# structure of the data frame
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
# using str()
print(str(friend.data))

输出:

'data.frame':    5 obs. of  2 variables:
 $ friend_id  : int  1 2 3 4 5
 $ friend_name: chr  "Sachin" "Sourav" "Dravid" "Sehwag" ...
NULL

数据框中的数据汇总

在 R 数据框中,可以通过应用summary()函数获得数据的统计摘要和性质。它是一个通用函数,用于对各种模型拟合函数的结果进行结果汇总。该函数调用依赖于第一个参数的类的特定方法。

例子:

R

# R program to get the
# summary of the data frame
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
# using summary()
print(summary(friend.data))

输出:

friend_id friend_name       
 Min.   :1   Length:5          
 1st Qu.:2   Class :character  
 Median :3   Mode  :character  
 Mean   :3                     
 3rd Qu.:4                     
 Max.   :5   

R语言从数据框中提取数据

从数据框中提取数据意味着访问其行或列。可以使用列名从数据框中提取特定列。

例子:

R

# R program to extract
# data from the data frame
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
 
# Extracting friend_name column
result <- data.frame(friend.data$friend_name)
print(result)

输出:

friend.data.friend_name
1                  Sachin
2                  Sourav
3                  Dravid
4                  Sehwag
5                   Dhoni

展开数据框

R 中的数据框可以通过向已经存在的数据框添加新的列和行来扩展。

例子:

R

# R program to expand
# the data frame
 
# creating a data frame
friend.data <- data.frame(
    friend_id = c(1:5),
    friend_name = c("Sachin", "Sourav",
                    "Dravid", "Sehwag",
                    "Dhoni"),
    stringsAsFactors = FALSE
)
 
# Expanding data frame
friend.data$location <- c("Kolkata", "Delhi",
                       "Bangalore", "Hyderabad",
                       "Chennai")
resultant <- friend.data
# print the modified data frame
print(resultant)

输出:

friend_id friend_name  location
1         1      Sachin   Kolkata
2         2      Sourav     Delhi
3         3      Dravid Bangalore
4         4      Sehwag Hyderabad
5         5       Dhoni   Chennai

在 R 中,可以对数据框执行各种类型的操作,例如访问行和列、选择数据框的子集、编辑数据框、删除数据框中的行和列等。请参阅 R 中的数据框操作了解可以在数据帧上执行的所有类型的操作。