R语言 数据框架操作
数据框架 是R的通用数据对象,用于存储表格数据。数据框架被认为是R编程中最流行的数据对象,因为以表格的形式分析数据会更舒服。数据框架也可以被教导为床垫,矩阵的每一列都可以是不同的数据类型。数据框架是由三个主要部分组成的,即数据、行和列。
可以对数据框架进行的操作有:
- 创建一个数据框架
- 访问行和列
- 选择数据框的子集
- 编辑数据框架
- 向数据框添加额外的行和列
- 在现有变量的基础上为数据框架添加新的变量
- 删除数据框中的行和列
创建一个数据框架
在现实世界中,一个DataFrame将通过从现有的存储中加载数据集来创建,存储可以是SQL数据库,CSV文件,以及Excel文件。数据框架也可以从R中的向量中创建。以下是一些可以用来创建数据框架的不同方法。
使用向量创建数据框: 为了创建一个数据框,我们使用R语言中的 data.frame() 函数。要创建一个数据框,请使用 data.frame() 命令,然后将你创建的每个向量作为参数传递给函数。
例子
# R program to illustrate dataframe
# A vector which is a character vector
Name = c("Amiya", "Raj", "Asish")
# A vector which is a character vector
Language = c("R", "Python", "Java")
# A vector which is a numeric vector
Age = c(22, 25, 45)
# To create dataframe use data.frame command and
# then pass each of the vectors
# we have created as arguments
# to the function data.frame()
df = data.frame(Name, Language, Age)
print(df)
输出
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
使用文件中的数据创建数据框: 数据框也可以通过导入文件中的数据来创建。为此,你必须使用名为 “read.table() “的函数。
语法
newDF = read.table(path="Path of the file")
要在R中从CSV文件中创建一个数据框架。
语法
newDF = read.csv("FileName.csv")
访问行和列
访问行和列的语法在下面给出。
df[val1, val2]
df = dataframe object
val1 = rows of a data frame
val2 = columns of a data frame
所以,这个 ‘val1 ‘和 ‘val2 ‘可以是一个数组的值,比如 “1:2 “或 “2:3 “等等。如果你只指定 df[val2] ,这只是指你需要从数据框中访问的那一组列。
例子。行选择
# R program to illustrate operations
# on a data frame
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
print(df)
# Accessing first and second row
cat("Accessing first and second row\n")
print(df[1:2, ])
输出
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
Accessing first and second row
Name Language Age
1 Amiya R 22
2 Raj Python 25
例子。列的选择
# R program to illustrate operations
# on a data frame
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
print(df)
# Accessing first and second column
cat("Accessing first and second column\n")
print(df[, 1:2])
输出
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
Accessing first and second column
Name Language
1 Amiya R
2 Raj Python
3 Asish Java
选择DataFrame的子集
在以下语法的帮助下,也可以根据某些条件创建一个数据框架的子集。
newDF = subset(df, conditions)
df = 原始数据框架
conditions = 某些条件
例子
# R program to illustrate operations
# on a data frame
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
print(df)
# Selecting the subset of the data frame
# where Name is equal to Amiya
# OR age is greater than 30
newDf = subset(df, Name =="Amiya"|Age>30)
cat("After Selecting the subset of the data frame\n")
print(newDf)
输出
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
After Selecting the subset of the data frame
Name Language Age
1 Amiya R 22
3 Asish Java 45
编辑DataFrames
在R中,DataFrames可以通过两种方式进行编辑:
通过直接赋值编辑数据帧: 与R中的列表一样,你可以通过直接赋值来编辑数据帧。
例子
# R program to illustrate operation on a data frame
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
cat("Before editing the dataframe\n")
print(df)
# Editing dataframes by direct assignments
# [[3]] accessing the top level components
# Here Age in this case
# [[3]][3] accessing inner level components
# Here Age of Asish in this case
df[[3]][3] = 30
cat("After edited the dataframe\n")
print(df)
输出
Before editing the data frame
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
After edited the data frame
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 30
使用edit()命令编辑数据帧:
按照给定的步骤来编辑数据帧。
第1步 :因此,你需要做的是创建一个数据框架的实例,例如,你可以看到这里创建了一个数据框架的实例,并通过使用命令 data.frame() 命名为 “myTable”,这样就创建了一个空数据框架。
myTable = data.frame()
第2步 :接下来我们将使用编辑函数来启动查看器。请注意,”myTable “数据框架被传回 “myTable “对象,这样,我们对这个模块所做的修改将被保存到原始对象中。
myTable = edit(myTable)
因此,当上述命令被执行时,它将弹出一个这样的窗口。
第3步 : 现在,这个小名册的表格已经完成了。
注意,通过点击它们的标签并输入你的改动来改变变量名称。变量也可以被设置为数字或字符。一旦DataFrame中的数据看起来像上面那样,关闭该表。更改会自动保存。
第4步: 通过打印来检查产生的数据框。
myTable
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
向数据框添加行和列
添加额外的行: 我们可以使用命令 rbind() 添加额外的行 。 其语法如下。
newDF = rbind(df, 你要添加的新行的条目 )
df = 原始数据框
请注意,在使用 rbind() 命令时,你必须注意你要添加的新行的条目,因为每一列条目中的数据类型应该与已经存在的行的数据类型相等。
例子
# R program to illustrate operation on a data frame
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
cat("Before adding row\n")
print(df)
# Add a new row using rbind()
newDf = rbind(df, data.frame(Name = "Sandeep",
Language = "C",
Age = 23
))
cat("After Added a row\n")
print(newDf)
输出
Before adding row
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
After Added a row
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
4 Sandeep C 23
添加额外的列: 我们可以使用 cbind() 命令添加额外的列 。 它的语法如下。
newDF = cbind(df, 你要添加的新列的条目 )
df = 原始数据框
例子
# R program to illustrate operation on a data frame
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
cat("Before adding column\n")
print(df)
# Add a new column using cbind()
newDf = cbind(df, Rank=c(3, 5, 1))
cat("After Added a column\n")
print(newDf)
输出
Before adding column
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
After Added a column
Name Language Age Rank
1 Amiya R 22 3
2 Raj Python 25 5
3 Asish Java 45 1
向DataFrame添加新的变量
在R语言中,我们可以在现有变量的基础上向数据框添加新的变量。要做到这一点,我们必须首先使用 library() 命令调用 dplyr 库 。 然后调用 mutate() 函数将在现有变量的基础上增加额外的变量列。
语法
library(dplyr)
newDF = mutate(df, new_var=[existing_var])
df = 原始数据框
new_var = 新变量的名称
existing_var = 你要进行的修改操作(例如对数值,乘以10)
例子
# R program to illustrate operation on a data frame
# Importing the dplyr library
library(dplyr)
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
cat("Original Dataframe\n")
print(df)
# Creating an extra variable column
# "log_Age" which is log of variable column "Age"
# Using mutate() command
newDf = mutate(df, log_Age = log(Age))
cat("After creating extra variable column\n")
print(newDf)
输出
Original Dataframe
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
After creating extra variable column
Name Language Age log_Age
1 Amiya R 22 3.091042
2 Raj Python 25 3.218876
3 Asish Java 45 3.806662
从数据框架中删除行和列
要删除一行或一列,首先需要访问该行或列,然后在该行或列前插入一个负号。它表示你必须删除该行或列。
语法
newDF = df[-rowNo, -colNo]
df = 原始数据框
例子
# R program to illustrate operation on a data frame
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
cat("Before deleting the 3rd row and 2nd column\n")
print(df)
# delete the third row and the second column
newDF = df[-3, -2]
cat("After Deleted the 3rd row and 2nd column\n")
print(newDF)
输出
Before deleting the 3rd row and 2nd column
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
After Deleted the 3rd row and 2nd column
Name Age
1 Amiya 22
2 Raj 25