在 R 中使用 dplyr 包创建、修改和删除列
在本文中,我们将讨论 R 编程语言中 dplyr 包中存在的 mutate函数,用于创建、修改和删除数据帧的列。
创建新列
可以通过附加新列或使用现有列来评估新列来插入列。默认情况下,列添加到最右侧。虽然可以使用.before和.after参数将列添加到任何所需的位置
句法:
mutate(dataframe , columns)
参数:
- 数据框是输入数据框
- 列是添加到数据框中的新列
- .before(默认情况下 = NULL)
- .after(默认情况下 = NULL)
例子:
R
library(dplyr)
# create a data frame
d <- data.frame(FirstName=c("Suresh", "Ramesh", "Tanya", "Sujata"),
Salary=c(50000, 60000, 70000, 80000),
Expenses=c(20000, 15000, 30000, 25000))
print(d)
# adding new columns
d <- mutate(d, Age=c(25, 28, 22, 27), Savings=Salary - Expenses)
print(d)
# adding a new column before FirstName
d <- mutate(d, Title=c("Mr", "Mr", "Ms", "Ms"), .before=FirstName)
print(d)
# adding a new column after FirstName
d <- mutate(d, LastName=c("Singh", "Pande", "Sinha", "Roy"),
.after=FirstName)
print(d)
R
library(dplyr)
# Create a data frame
d <- data.frame( FirstName = c("Suresh","Ramesh","Tanya","Sujata"),
Salary = c(50000,60000,70000,80000),
Expenses = c(20000,15000,30000,25000))
print(d)
# Delete Expenses column
d <- mutate(d,Expenses = NULL)
print(d)
R
library(dplyr)
# Create a data frame
d < - data.frame(FirstName=c("Suresh", "Ramesh", "Tanya", "Sujata"),
Salary=c(50000, 60000, 70000, 80000),
Expenses=c(20000, 15000, 30000, 25000))
print(d)
# Modify FirstName, Salary column
d < - mutate(d, FirstName=c("Mahesh", "Jignesh", "Ria", "Tanya"),
Salary=c(60000, 30000, 50000, 75000))
print(d)
输出:
FirstName Salary Expenses
Suresh 50000 20000
Ramesh 60000 15000
Tanya 70000 30000
Sujata 80000 25000
FirstName Salary Expenses Age Savings
Suresh 50000 20000 25 30000
Ramesh 60000 15000 28 45000
Tanya 70000 30000 22 40000
Sujata 80000 25000 27 55000
Title FirstName Salary Expenses Age Savings
Mr Suresh 50000 20000 25 30000
Mr Ramesh 60000 15000 28 45000
Ms Tanya 70000 30000 22 40000
Ms Sujata 80000 25000 27 55000
Title FirstName LastName Salary Expenses Age Savings
Mr Suresh Singh 50000 20000 25 30000
Mr Ramesh Pande 60000 15000 28 45000
Ms Tanya Sinha 70000 30000 22 40000
Ms Sujata Roy 80000 25000 27 55000
删除列
通过将所需列的值设置为NULL ,可以从现有数据框中删除列。
句法:
mutate(dataframe,columns = NULL)
范围:
- 只需要一个参数就是要删除的列名
例子:
R
library(dplyr)
# Create a data frame
d <- data.frame( FirstName = c("Suresh","Ramesh","Tanya","Sujata"),
Salary = c(50000,60000,70000,80000),
Expenses = c(20000,15000,30000,25000))
print(d)
# Delete Expenses column
d <- mutate(d,Expenses = NULL)
print(d)
输出:
FirstName Salary Expenses
Suresh 50000 20000
Ramesh 60000 15000
Tanya 70000 30000
Sujata 80000 25000
FirstName Salary
Suresh 50000
Ramesh 60000
Tanya 70000
Sujata 80000
修改列
可以通过为所需列分配新值来修改现有列。
句法:
mutate(dataframe,column_name=new_values)
参数:它将接受两个参数
- 数据框是输入数据框
- column_name 是要修改值的列的名称
例子:
R
library(dplyr)
# Create a data frame
d < - data.frame(FirstName=c("Suresh", "Ramesh", "Tanya", "Sujata"),
Salary=c(50000, 60000, 70000, 80000),
Expenses=c(20000, 15000, 30000, 25000))
print(d)
# Modify FirstName, Salary column
d < - mutate(d, FirstName=c("Mahesh", "Jignesh", "Ria", "Tanya"),
Salary=c(60000, 30000, 50000, 75000))
print(d)
FirstName Salary Expenses
Suresh 50000 20000
Ramesh 60000 15000
Tanya 70000 30000
Sujata 80000 25000
FirstName Salary Expenses
Mahesh 60000 20000
Jignesh 30000 15000
Ria 50000 30000
Tanya 75000 25000