R语言 使用dplyr包创建、修改和删除列
在这篇文章中,我们将讨论R编程语言中dplyr包中的mutate函数,以创建、修改和删除数据框架的列。
创建新列
列可以通过添加一个新的列或使用现有的列来评估一个新的列来插入。默认情况下,列被添加到最右边。尽管可以使用 .before 和 .after 参数将列添加到任何需要的位置
语法
mutate(dataframe , columns)
参数
- dataframe是输入的数据框架
- 列是被添加到数据框架中的新列
- .before(默认 = NULL)
- .after(默认 = NULL)
例子
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)
输出
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)
参数
- 它只需要一个参数,就是要删除的列名。
例子
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)
参数: 它将接受两个参数
- dataframe是输入的数据框架
- column_name是要修改数值的列的名称
例子
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